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

JavascriptJqueryCasting

Javascript Problem Overview


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;
$("replies").text(newValue);

What's happening is it is appearing like:

81

then

811

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

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

Parse int is the tool you should use here, but like any tool it should be used correctly. When using parseInt you should always use the radix parameter to ensure the correct base is used

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

Solution 3 - Javascript

The integer is being converted into a string rather than vice-versa. You want:

var newValue = parseInt(currentValue) + 1

Solution 4 - Javascript

parseInt didn't work for me in IE. So I simply used + on the variable you want as an integer.

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

Solution 5 - Javascript

In regards to the octal misinterpretation of .js - I just used this...

parseInt(parseFloat(nv))

and after testing with leading zeros, came back everytime with the correct representation.

hope this helps.

Solution 6 - Javascript

to increment by one you can do something like

  var newValue = currentValue ++;

Solution 7 - Javascript

Your code should like this:

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

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

Hacks N Tricks

Solution 8 - Javascript

Simply, add a plus sign before the text value

var newValue = +currentValue + 1;

Solution 9 - Javascript

var month = new Date().getMonth();
var newmon = month + 1;
$('#month').html((newmon < 10 ? '0' : '') + newmon );

I simply fixed your month issue, getMonth array start from 0 to 11.

Solution 10 - Javascript

You can multiply the variable by 1 to force JavaScript to convert the variable to a number for you and then add it to your other value. This works because multiplication isn't overloaded as addition is. Some may say that this is less clear than parseInt, but it is a way to do it and it hasn't been mentioned yet.

Solution 11 - Javascript

You can use parseInt() method to convert string to integer in javascript

You just change the code like this

$("replies").text(parseInt($("replies").text(),10) + 1);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrballView Question on Stackoverflow
Solution 1 - JavascriptDiodeus - James MacFarlaneView Answer on Stackoverflow
Solution 2 - JavascriptJon PView Answer on Stackoverflow
Solution 3 - JavascriptChuckView Answer on Stackoverflow
Solution 4 - JavascriptGareth NelView Answer on Stackoverflow
Solution 5 - JavascriptBill OrtellView Answer on Stackoverflow
Solution 6 - JavascriptalexView Answer on Stackoverflow
Solution 7 - JavascriptrajeevView Answer on Stackoverflow
Solution 8 - JavascriptAgorrecaView Answer on Stackoverflow
Solution 9 - JavascriptAdeel IshfaqView Answer on Stackoverflow
Solution 10 - JavascriptRyanView Answer on Stackoverflow
Solution 11 - JavascriptCodemakerView Answer on Stackoverflow