5
I have this code where I’m getting the values correctly from the form (I made a alert
with each item and is getting it right). Only when saving in the variable the sum is not certain, qdo dou alert
summed up appears NaN
. Follow the excerpt of my code:
function somaPesos() {
var soma = 0;
var itens = document.getElementsByClassName("soma");
for (i = 0; i < itens.length; i++) {
var item = parseInt(itens[i].value);
alert(item);
soma = soma + item;
}
alert(soma);
return false;
}
I have tested your code and it seems to me that it is correct: http://jsfiddle.net/d47yxjn3/1/
– CesarMiguel
There is something here that I don’t understand... If you are giving the Alert of your item while going through the cycle, where is the mistake more specifically? Is to calculate the sum?
– CesarMiguel
I tested here also getting the value through html, to check if the problem was this and everything works as expected, you just have to fill in all fields, otherwise the error occurs as you quoted. Check the example.
– Fernando Leal
I think that’s the problem you’re pointing at @Fernando
– CesarMiguel
Since you are getting the values from a class selector (which is a bit risky), it may be that your class = "sum", is being used somewhere you don’t expect and the value is coming empty/Undefined, check if this is not happening? (or if that’s not happening? as @Cesarmiguel would say, hehe)
– Fernando Leal