Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
24
rated 0 times [  30] [ 6]  / answers: 1 / hits: 6261  / 10 Years ago, thu, september 11, 2014, 12:00:00

I'm trying to sum values from an input array but the sum it keeps returning NAN



var sum = 0    //i have also tried parse(0,10);
$(qty_name).each(function(){
var aValue = $(this).val(); //i have tried parseInt($(this).val(), 10)
sum += aValue; //i have tried sum = parseInt(sum) + parseInt(aValue)
});
alert(sum);


i keep getting NaN. I'm coming from a php background so i've never had to deal with type casting. Please what am i missing?


More From » jquery

 Answers
2

That is because your other qty_name do not have perfect integer value. which results to NAN for such values. You need to parse the values to int(or float) for doing any mathematical calucations:



 var sum = 0;
$(qty_name).each(function(){
sum += parseInt($(this).val()) || 0;
});
alert(sum);

[#42566] Wednesday, September 10, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
bradford

Total Points: 709
Total Questions: 117
Total Answers: 91

Location: Sao Tome and Principe
Member since Wed, Dec 21, 2022
1 Year ago
;