2
<div class="sub_total">34,814.64</div>
<div class="sub_total">39,800.64</div>
<div class="sub_total">14,004.04</div>

I want to sum by class I tried by this way

var sum = 0;

$('.sub_total_w_tax').each(function(){
     sum += parseFloat($(this).text());
});

but the result is wrong

2 Answers 2

1

You need to remove the commas before adding.

var sum = 0;

$('.sub_total').each(function(){

sum += parseFloat($(this).text().replace(/,/g , ""));

});
alert(sum);

Fiddle

Sign up to request clarification or add additional context in comments.

Comments

1

Remove the , from the string, otherwise while it parsing 34,814.64 will get as 34

var sum = 0;

$('.sub_total').text(function(i, v) {
  sum += parseFloat(v.replace(',', ''));
});

$('#result').text('Result : ' + sum);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div class="sub_total">34,814.64</div>
<div class="sub_total">39,800.64</div>
<div class="sub_total">14,004.04</div>
<div id="result"></div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.