[ACCEPTED]-sum all values for table column based on class-jquery
Accepted answer
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
$(document).ready(function(){
$('.price').each(function() {
calculateSum();
});
});
with
$(calculateSum);
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.