I have some divs that have values. I want to sum in one <h3>
The probem in my code is that I get the last div value and cannot sum the other.
Html code:
<div class="cart-footer">
<div class="order-tools">
<h3 id="total">
</h3>
</div>
<div class="cash-out">
</div>
</div>
Jquery:
var sum = 0;
$('#item-total').each(function(){
var val = $.trim($(this).text());
if (val) {
val = parseFloat(val.replace(/^\$/, ""));
sum += !isNaN(val) ? val : 0;
}
});
$("#total").html(sum + "$");
You can see #item-total
in this code:
$(".cart-body").append(function(){
return "<div id='cart-list'><div class='product-name'>"+personObject.name+"</div><div class='product-tools'><input type='number' data-id='1' value='1' min='1'></input><label id='price'>Price: "+personObject.price+"</label><label data-value='"+personObject.count * personObject.price+"' id='item-total'>"+personObject.count * personObject.price+"</label></div></div>";
});