0

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

Ali_dotNet
  • 3,219
  • 10
  • 64
  • 115
  • 1
    Did you try to debug to see how is your like_value ? Just add `console.log(like_value);` and type F12 to see the console. – Denys Séguret Jul 20 '12 at 12:53
  • I write it using alert, it is fine in like_value = like_value.substring(1, like_value.length - 1); , but after parseInt I get NaN – Ali_dotNet Jul 20 '12 at 12:54
  • 1
    This isn't relevant to your problem, but it is useful advise; browser sniffing is *sooo* 1500's. You should look at using ***feature detection*** instead (e.g. `"textContent" in document.createElement("div")` rather than `navigator.userAgent.indexOf("Firefox") != -1` – Matt Jul 20 '12 at 12:55
  • Why are you doing this substring operation Are you sure you have at least 3 chars in your number ? – Denys Séguret Jul 20 '12 at 12:56
  • @Ali_dotNet: So what is the exact value being `alert`'d? – Matt Jul 20 '12 at 12:56
  • it displays NaN after parseInt, originally I have (11), I use substring to remove ( and ), then I try to covert "11" to 11, is it right? – Ali_dotNet Jul 20 '12 at 12:58
  • You should trim it first, because if you have spaces around, the parentheses wouldn't be removed by the substring operation : like_value=like_value.trim(); – Denys Séguret Jul 20 '12 at 13:02
  • Please post html, mostly the part with mySpan I am interested in. – Angel Jul 20 '12 at 13:52

2 Answers2

1

Trying printing out what your like_value is. NaN is returned if the string can not be converted to a number, so something has to be wrong with your string

CBaker
  • 830
  • 2
  • 10
  • 25
1

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 :

var like_value = document.getElementById('mySpan').innerHTML;
like_value = like_value.replace(/[\n\r\t\(\)]/g,); // this also remove the parenthesis
var real_number = parseInt(like_value, 10);
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758