I’m trying to convert a numeric string to a number, then I want to increase (or decrease) its value, and finally it should be written back to its original place (in a span). But I’m always getting NaN when I try parseInt:
var like_value = "0";
if (navigator.userAgent.indexOf("Firefox") != -1)//firefox
like_value = document.getElementById('mySpan').innerHTML;
else
like_value = document.getElementById('mySpan').innerText;
like_value = like_value.substring(1, like_value.length - 1);
var real_number = parseInt(like_value, 10);
real_number++;
alert(real_number);//it displays NaN !!!
if (navigator.userAgent.indexOf("Firefox") != -1)//firefox
document.getElementById('mySpan').innerHTML = real_number;
else
like_value = document.getElementById('mySpan').innerText = real_number;
I think there should be a problem in my substring, of course I get what I expect but perhaps it cannot be correctly converted to int
If you have
" (11)"before the substring operation, you have"(11"after as it removes the first and last letter.This parses as NaN.
The problem is that there are meany possible reasons to have spaces or new lines around your string.
I’d recommend to clean your string like this :