Javascript Ajax jQuery Html PHP Example Quiz New MORE

jQuery Auto Calculate Profit in a table

<!Doctype html>
<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
    <table>
        <tr>
            <td><input type="number" class="product-buying-price"></td>
            <td><input type="number" class="product-selling-price"></td>
            <td class="net-profit"></td>
        </tr>
    </table>
    <script>
        $('.product-buying-price, .product-selling-price').on('blur', function(e) {
            var row = $(this).closest('tr');
            var buying_pr = $('.product-buying-price', row),
                selling_pr = $('.product-selling-price', row),
                net_profit = $('.net-profit', row);
            buy_pr = parseFloat(buying_pr.val());
            sell_pr = parseFloat(selling_pr.val());
            if (!isNaN(buy_pr) && !isNaN(sell_pr)) {
                net_profit.text((sell_pr - buy_pr).toFixed(2));
            }
        });
    </script>
</body>
</html>

Run it yourself

Demo




Download Source Code