3

I got a problem, I try to return a value with innerHTML but I got a NaN. I know my variable is not a number but why he keep telling me that ?

function checkWord(id, nameOutput){
    var pattern = new RegExp("\\b(hello|world)\\b", "i");
    var fieldValue = document.getElementById(id).value;
    var result = fieldValue.search(pattern);
    if (result != -1){
        document.getElementById(nameOutput).style.color = "green";
        document.getElementById(nameOutput).innerHTML = +fieldValue+ " is correct";
    }
    else{
        document.getElementById(nameOutput).style.color = "red";
        document.getElementById(nameOutput).innerHTML = +fieldValue+ " is incorrect";
    }
    if(fieldValue == null || fieldValue == ""){
        document.getElementById(nameOutput).style.color = "orange";
        document.getElementById(nameOutput).innerHTML = "Empty field";
    }     
} 

I got always NaN is incorrect or NaN is correct why i can't get the value print ?

If i wrote like this :

document.getElementById(nameOutput).innerHTML ="" +fieldValue+ " is correct";
document.getElementById(nameOutput).innerHTML ="" +fieldValue+ " is incorrect";

Everything works well !! but why i need to write this "" in the innerHTML

Did I do something wrong ?

adricadar
  • 9,971
  • 5
  • 33
  • 46
Pierre
  • 675
  • 1
  • 8
  • 18
  • What is the `fieldValue` actually? Are you sure the value CAN be parsed as number? Otherwise it would be `NaN`, just like `parseInt('qwerty')` do. – Leo May 16 '15 at 04:45

6 Answers6

4

Change

document.getElementById(nameOutput).innerHTML = +fieldValue+ " is correct";

to

document.getElementById(nameOutput).innerHTML = fieldValue + " is correct";

because = +fieldValue is otherwise missing something to add/concatenate to fieldValue. So, instead a cast is made to a number which results in an undefined number, or NaN.

When you tried

document.getElementById(nameOutput).innerHTML ="" +fieldValue+ " is correct"; 

you supplied something to concatenate to fieldValue, namely an empty string, so no number conversion was performed on fieldValue.

Drakes
  • 23,254
  • 3
  • 51
  • 94
2

This is happening because you make a conversion (+fieldValue) to number. When you add "" in front of you make a conversion to string (""+fieldValue). You can read more about JavaScript type conversion here.

adricadar
  • 9,971
  • 5
  • 33
  • 46
0

Use this and i hope everything work good. Don't use "+" sign before fieldValue because its try to concatenate with previous one which is not defined yet.

    document.getElementById(nameOutput).innerHTML = fieldValue+ " is incorrect";

and also make sure about "fieldValue" is coming or not by alerting it before above line like.

alert("Value of "+fieldValue);
document.getElementById(nameOutput).innerHTML = fieldValue+ " is incorrect";
0

Here is a correct version with refactored code. Hope it will help you.

    function checkWord(id, nameOutput){
      var pattern = new RegExp("\\b(hello|world)\\b", "i"),
          fieldValue = document.getElementById(id).value,
          result = fieldValue.search(pattern),
          output = document.getElementById(nameOutput),
          color, ohtml;
          if (result!= -1){
            color = 'green';
            ohtml = fieldValue + " is correct"
          }
         else{
           color = "red";
           ohtml = fieldValue + " is incorrect";
         }
        if(fieldValue == null || fieldValue == ""){
          color = "orange";
          ohtml = "Empty field";
        }   
         output.style.color = color;
         output.innerHTML = ohtml;              
   } 
prashantsahni
  • 2,128
  • 19
  • 20
0

Thanks for all your answers and explications..

You improved my knowledge. =)

And everything works fine know with :

document.getElementById(nameOutput).innerHTML = fieldValue+ " is correct";

document.getElementById(nameOutput).innerHTML = fieldValue+ " is incorrect";
Pierre
  • 675
  • 1
  • 8
  • 18
0

I had this error when printing a variable number to innerHTML and I solved the problem by assigning an initial value to the variable I wanted to print.

Yusuf
  • 771
  • 1
  • 6
  • 8