I want to get the total value from <div>
's with the class itemPrice
My HTML
<div class="itemPrice">1,000</div>
<div class="itemPrice">2,000.50</div>
<div class="itemPrice">3,000</div>
<div class="itemPrice">1,020.54</div>
<div class="itemPrice">4,200</div>
<div class="itemPrice">3,500</div>
<div class="total">?????</div>
My JavaScript
$(document).ready(function() {
var sum = 0;
$(".itemPrice").each(function() {
var val = $.trim($('.itemPrice').text());
// val = val.split(",")
if (val) {
val = parseFloat( val.replace( /^\$/, "" ) );
sum += !isNaN( val ) ? val : 0;
}
});
$('.total').text(sum);
});
But it does not work because of the comma(,).