I need to sum some money value, i have this html
<div class=''>54,44</div>
<div class=''>34,10</div>
<div class=''>44,00</div>
<div class=''>14,50</div>
How can i make an alert with the sum of these div text?? ( the right sum!! ;-) ) Many thanks!
Add some class like money
(or use $("div")
selector) and do something like this:
var sum = 0;
$(".money").each(function(){
sum += parseFloat($(this).text().replace(",", "."));
});
alert(sum);
Firstly, its worthwhile putting a data-attribute on each div with the raw amount, devoid of any locale-specific formatting.
eg/
<div data-amount="55.44">54,44</div>
Then assuming these div's are identifiable you can simply do
var total = 0;
$('some-selector-for-your-divs').each(function(){
total += parseFloat($(this).data('amount'));
});
Edit: Duh! parseInt on monetery amounts fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With