[Solved] jQuery sum table cells [duplicate]


Change $('.price') to $(this), to refer the element inside the callback.

$(document).ready(function() {
    var sum = 0;
    var quantity = 0;
    $('.price').each(function() {
        var price = $(this);
        var q = price.closest('tr').find('.quantity').val();
        sum += parseInt(price.val()) * parseInt(q);
        quantity += parseInt(q);
    });

    $('#total_price').html(sum);
    $('#total_quantity').html(quantity);
});

Fiddle Demo

2

solved jQuery sum table cells [duplicate]