11

I have a variable that stores a innerHTML text in it

var text = document.getElementById("textID").innerHTML; // <-- textID is actually a number

The "text" is actually just a number but I guess javascript still thinks it's a string.

I want to add + 1 to the variable text, but it just adds a new letter instead of increasing the number

For example : 0 + 1 = 01 --> 01 + 1 = 011 and so on...

Here's the code I tried this with:

text = text + 1;

How can I make it so it increases the number instead of adding new letters? (1+1 = 2, etc)

0

1 Answer 1

30

Under the assumption that you are absolutely sure that the number is a decimal integer. Either

text = +text + 1;

Or

text = parseInt(text, 10) + 1;

Or

text = Number(text) + 1;
Sign up to request clarification or add additional context in comments.

1 Comment

text = (parseInt(this.text) + 1).toString(); or text = String(parseInt(this.text) + 1)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.