You will need to check for numbers (ints) as one of your tds is empty it will return an empty string or with the parseInt function it will return NaN. This will break any calculations you do later on.
The following requires jQuery but can easily be done with pure JS. It will check to see if it's a number and if not it will/should return 0 for those that aren't numbers and convert the 'string' numbers to actual ints for adding:
var total = 0;
$('td').each(function() {
var num = parseInt(this.innerHTML, 10); // Converts string to int. Added Radix as @JonP kindly pointed out.
if (!isNaN(num)) { // Checks if all int are numbers and not NaN (not a number)
total = total + num; // Just for clarity's sake this is written out fully
}
});
alert(total);
Non jQuery way is this:
var rows = document.getElementsByClassName('numeric'),
total = 0;
for(var i = 0; i < rows.length; i++) {
var num = parseInt(rows[i].innerHTML, 10);
if (!isNaN(num)) {
total = total + num;
}
}
alert(total);
EDIT: As @jonP pointed out if using parseInt(num, 10) you need to add a radix - which is the second parameter. Link to article explaining Radix. So line should read parseInt(this.innerHTML, 10)