[ACCEPTED]-sum all values for table column based on class-jquery

Accepted answer
Score: 48

You want to use text() instead of this.value (since <td>s don't 3 have a "value"):

var sum = 0;
// iterate through each td based on class and add the values
$(".price").each(function() {

    var value = $(this).text();
    // add only if the value is number
    if(!isNaN(value) && value.length != 0) {
        sum += parseFloat(value);

Also, you're looping 2 over your .price elements (calling calculateSum) multiple 1 times. You can replace

    $('.price').each(function() {


Score: 5

I have a JQuery plugin, Sumtr, that handles this 2 pretty well if you want a summary row.

Here 1 is your example with the plugin.

More Related questions