Javascript problem in converting value to int

Asked

Viewed 84 times

0

I need two functions that pick up the value of one input and send it to another input this value +2, had already asked this question here but went to +3 and got the solution, however I did another function with another name and other fields, and the result was NaN. Here’s the +3 code that works:

function tenta() {
    if(document.getElementById('ciclo').value == "")
    {
        document.getElementById('ciclo2').value = "2013"; //placeholder
         document.getElementById('ciclo2').style.color = "#bfbfbf";
    }else
    {
        document.getElementById('ciclo2').style.color = "black";
        var x = document.getElementById('ciclo').value;
        document.getElementById('ciclo2').value = parseFloat(x) + 3;
    }
}

and the equal function, but returns NaN:

    function numeros2()
        {
            if(document.getElementById('ciclo3').value == "")
            {
               document.getElementById('ciclo4').value = "2013"; //placeholder;
                 document.getElementById('ciclo4').style.color = "#bfbfbf";
            }else
            {
                document.getElementById('ciclo4').style.color = "black";
                var xy = parseInt(document.getElementById('ciclo4').value);
                document.getElementById('ciclo4').value = xy + 2;
            }
        }

I’ve tried the parseFloat, but it also doesn’t work.

  • Empty values are Nan itself. I think your problem is in the if. Lse, it doesn’t have much to do with the value. You test the cycle, but you change the cyclo2. Then test cyclo3, but apply to cyclo4. Is that right? Put a [mcve] on JS Fiddle and send us the link to test.

  • It’s the same HTML as the other question?

  • Yes Sergio and the same HTML. The inputs have a onkeyup.

  • Hello, I made a jsfiddle but I couldn’t replicate its embarrassment. http://jsfiddle.net/q6jGr/196/ You can create to help then.

  • is exactly that Tiago Gomes, but instead of being necessary to click, is made an onkeyup. but the mistake is just that, one works on the other.

2 answers

4


The problem is that parseInt("") gives NaN, that is, when the string is empty the parseInt gives that, what does it mean "Not to NUmber" ("It’s not a number").

You gotta do it like this: var xy = parseInt(ciclo3.value || 0, 10);. That way he chooses zero, in case the value is an empty string. You must take into account that empty string validates as "false" when converted to Boolean.

jsFiddle: https://jsfiddle.net/kcqvfq14/1/

  • Sérgio, the output of this jsfiddle is not what is intended, what I wanted is to put for example 2010 in a field and in the field that will receive the function is to add 2 more, ie the value of the input will be 2012. As I am new in javascript I do not understand this "0,10".

  • @2016 programmeri edited. I had fixed the problem with parseint but I also needed to fix for cyclo3.

  • It worked. Thank you :)

0

It may be that the document.getElementById("ciclo").value is not returning a String. Not all DOM objects have the "value property".

Check which object is being returned in this portion of the code and use the appropriate form to get the data you need.

If you clarify which Node is returned from document.getElementById("ciclo") and HTML will be easier to post a more specific answer to your problem.

Browser other questions tagged

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