Sum of values from different divs Jquery | Codexpress Programming Blog - Tutorials about, PHP, MySQL and Web Development

Sum of values from different divs Jquery


   I am loading dynamically divs that have a .totalprice class. At the end, It will sum of the values from all of the .totalprice.










For <div> Elements:

<div class='totalprice'>1.25</div>
 <div class='totalprice'>0.25</div>
 <div class='totalprice'>3.00</div>
 <div class='totalprice'>2.50</div>
 <div class='totalprice'>0.01</div>

 <script>
 var sum = 0.0;
 $('.totalprice').each(function()
 {
     sum += parseFloat($(this).text());
 });
 alert(sum);
 </script>

For <input> Elements (inputs, checkboxes, etc.):
 
Alternatively, if you are looking for an integer, you can use the parseInt() function.
see working demo http://jsfiddle.net/PQ6Lk/