Friday, 15 May 2015

javascript - Add values inside same class and return the sum -



javascript - Add values inside same class and return the sum -

hello people here code below...

var total = 0; var find_total; function total_val(find_total){ $(find_total).each(function() { total += parseint($(this).text()); homecoming total; }); }

iam calling function...

$('#total_price').val(total_val('.price'));

#total_price , .price keeps changing different div ids , class ... return total; not work, ways prepare this??

you returning total value within .each() function, not homecoming while expecting value function total_val(),

try,

function total_val(find_total){ var total = 0; //remove global variable total , utilize this. $(find_total).each(function() { total += parseint($(this).text()); }); homecoming total; }

or utilize .reduce() create code simple.

function total_val(find_total){ homecoming $(find_total).map(function() { homecoming parseint($(this).text()); }).get().reduce(function(a,b){ homecoming a+b; }); }

even simpler,

function total_val(find_total){ homecoming $(find_total).get().reduce(function(a,b){ homecoming a+ parseint($(b).text(),10); },0); }

javascript jquery html

No comments:

Post a Comment