rball rball - 9 months ago 45
Javascript Question

How do I add an integer value with javascript (jquery) to a value that's returning a string?

I have a simple html block like:

<span id="replies">8</span>

Using jquery I'm trying to add a 1 to the value (8).

var currentValue = $("#replies").text();
var newValue = currentValue + 1;

What's happening is it is appearing like:




not 9, which would be the correct answer. What am I doing wrong?


parseInt() will force it to be type integer, or will be NaN (not a number) if it cannot perform the conversion.

var currentValue = parseInt($("#replies").text(),10);

The second paramter (radix) makes sure it is parsed as a decimal number.