Click here to Skip to main content
15,888,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i hava jquery to calculating the sum of numbers but sum will be displayed in append manner
the jquery code given below


JavaScript
$(.cprice).each(function (i, obj) {
           $(obj).html().trim();
           total = (total) + (parseInt($(obj).html().trim()));
          
       })
       $(#tprice).html(total);


eg: the number are 1000, 2000, 3000
the result will be shown in 100020003000

i need to display the sum is 6000
Posted
Updated 4-Aug-15 2:00am
v4

try this. parse the "total" as Int before adding.

JavaScript
$(.cprice).each(function (i, obj) {
           $(obj).html().trim();
           total = (parseInt(total) + (parseInt($(obj).html().trim())));
          
       })
       $(#tprice).html(total);


hope it works.
 
Share this answer
 
v2
replace third line with the follwoing code. as your code is behaving like string concatination u need to convert it to int type.

JavaScript
total = parseInt(total + parseInt($(obj).html().trim()));


Thanks
 
Share this answer
 
v3
Comments
Athul MS 5-Aug-15 8:45am    
Mr mukesh its not working the working line is given below

total = parseInt(total + parseInt($(obj).html().trim()));

Thanks
Mukesh Pr@sad 5-Aug-15 9:39am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900