admin管理员组文章数量:1356826
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 beNaN
, just likeparseInt('qwerty')
do. – Leo Commented May 16, 2015 at 4:45
6 Answers
Reset to default 4Change
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.
本文标签: javascriptinnerHTML return NaN with textStack Overflow
版权声明:本文标题:javascript - innerHTML return NaN with text - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744068747a2585495.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论