javascript - jquery add the values inside same div id -
hello people iam trying add together ,all value found within same div...
<div id="price">20</div> <div id="price">25</div> <div id="price">10</div> <div id="price">10</div> <div id="price">5</div> so in output iam supposed total value=70 .. tried using each , map both did not work expected...
var total = 0; $( "#price" ).map(function() { var total + = number($(this).val()); }).get(); console.log(total); iam getting error unexpected token += same in case of each ,, ways prepare this??
the error got due spacing in between + , = opeator. ids should unique, utilize class instead.try this:
dom:
<div class="price">20</div> <div class="price">25</div> <div class="price">10</div> <div class="price">10</div> <div class="price">5</div> js:
var total = 0; $( ".price" ).each(function() { total += parseint($(this).text()); }); console.log(total); working demo
this not recommended can utilize attribute-value target elements same ids:
var total = 0; $( "[id=price]" ).each(function() { total += parseint($(this).text()); }); console.log(total); demo attribute-value
javascript jquery html
No comments:
Post a Comment