Sprunkas Sprunkas - 11 months ago 41
Javascript Question

Calculating sum only for relevant row

So I have problem which I can't solve for hours now..

When I press on button "PridÄ—ti prekÄ™" then one row is dynamically added to the table and when I write numbers in "Kiekis" and "Kaina" columns nothing happens in "Suma" column (It only happens to dynamically added row).

Piece of jQuery:

$(document).on('keyup', '#quantity, #price', function() {
$('#total').val(($('#quantity').val() * $('#price').val()).toFixed(2));

Please look at JSFiddle how is "working" my code:


Thanks for help in advance!

P.S I tried to change from div ID total, to div class total, then it works, but it applies to all rows instead to relevant.

wot wot

You were referencing everything with id's.

    $(document).on('keyup', '.quantity, .price', function() {
        var row = $(this).closest('tr').get(0);
        var rowPrice = $(row).find('.price').val();
        var rowQuantity = $(row).find('.quantity').val();

        $(row).find('.total').val( (rowPrice * rowQuantity).toFixed(2) )