How to take the value with getElement and assign to a variable?

Asked

Viewed 3,412 times

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;
}
  • 2

    I have tested your code and it seems to me that it is correct: http://jsfiddle.net/d47yxjn3/1/

  • 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?

  • 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.

  • I think that’s the problem you’re pointing at @Fernando

  • 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)

4 answers

4


This error is probably occurring because some input containing the "sum" class is valueless. You can do a check before, so it doesn’t occur anymore.

function somaPesos() {
  var soma = 0;
  var itens = document.getElementsByClassName("soma");

  for (i = 0; i < itens.length; i++) {
    if (!isNaN(parseInt(itens[i].value))) {
      var item = parseInt(itens[i].value);
      soma += item;
    }
  }

  alert(soma);
  return false;
}
Valor 1: <input type="text" class="soma"><br>
Valor 2: <input type="text" class="soma"><br>
Valor 3: <input type="text" class="soma">
<input type="button" onclick="somaPesos()" value="Somar">

What I did was to parse Int in the input, because empty the Isnan function returns false and then denied to it and enter and assign the value to sum.

  • 2

    That’s right, people! Thank you to everyone who answered.

2

Try this:

<script language="javascript" type="text/javascript">
        function somaPesos(){
            var soma = 0;

            var itens = document.getElementsByClassName("soma");
            for(i = 0; i < itens.length; i++){
                if(!isNaN(itens[i].value)){
                   var item = parseInt(itens[i].value);
                   alert(item);
                   soma = soma + item;
                }                 
            }
            alert(soma);

            return false;
        }
</script>

1

This mistake NaN (Not a Number) happens when you try to do an operation ( + can be a concatenation) with a variable that is not number.

You probably didn’t enter any number in input and then he tries to add a number with something.

  • it seems to me that the error does not pass in the way Alert is treating the variable

  • Which browser has this problem with Alert @Earendul?

  • I corrected my answer. Sorry for the failure.

0

In html :

<input id="ipTexto" type="text"></input>

No Js:

var inputText = document.getElementById('ipTexto');

At this point, inputText has the integer element. If you need the value, use :

console.log(inputText.value);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.