最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - innerHTML return NaN with text - Stack Overflow

programmeradmin3浏览0评论

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 ?

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 ?

Share Improve this question edited May 16, 2015 at 7:25 adricadar 10.2k5 gold badges34 silver badges47 bronze badges asked May 16, 2015 at 4:32 PierrePierre 6751 gold badge8 silver badges19 bronze badges 1
  • 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 Commented May 16, 2015 at 4:45
Add a ment  | 

6 Answers 6

Reset to default 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.

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.

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 ing or not by alerting it before above line like.

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

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

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";

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.

发布评论

评论列表(0)

  1. 暂无评论