I am using this bit of code to add the values in a table column which works pretty well until it encounters a null td cell with a value. From that point on in the loop, I receive a NaN error in my alert. I'm wondering how to ignore these non-numeric values or replace them with zero for the calculation?
jQuery(function() {
var MarketCapTotal = 0;
// loop through the table
jQuery('#grdWatchlistname tbody tr').each(function() {
// replace the dollar signs and commas
var MarketCap = (jQuery('td:nth-child(4)', jQuery(this)).html
().replace('$', '').replace(/[^a-zA-Z 0-9]+/g, ''));
var td4th = jQuery('td:nth-child(4)', jQuery(this));
MarketCapTotal += parseInt(MarketCap);
alert(MarketCapTotal);
});
})开发者_开发技巧;
Try this:
MarketCapTotal += isNaN(MarketCap) ? 0: parseInt(MarketCap, 10);
精彩评论