JayDee
JayDee

Reputation: 1643

How to increment a numeric string by +1 with Javascript/jQuery

I have the following variable:

pageID = 7

I'd like to increment this number on a link:

$('#arrowRight').attr('href', 'page.html?='+pageID);

So this outputs 7, I'd like to append the link to say 8. But if I add +1:

$('#arrowRight').attr('href', 'page.html?='+pageID+1);

I get the following output: 1.html?=71 instead of 8.

How can I increment this number to be pageID+1?

Upvotes: 20

Views: 49471

Answers (8)

jackwanders
jackwanders

Reputation: 16020

Just change your order of operations by wrapping your addition in parentheses; if pageID is already a number, parseInt() isn't necessary:

$('#arrowRight').attr('href', 'page.html?='+(pageID+1));

Demo

Upvotes: 1

sookool99
sookool99

Reputation: 262

All these solutions assume that your number you want to add 1 to is within the machine precision for an integer. So if you have a large enough number within that string when you add 1 to it won't change the number.

For Example:

parseInt('800000000000000000', 10) + 1 = 800000000000000000

So I wrote a quick solution to the problem

function addOne(s) {
    let newNumber = '';
    let continueAdding = true;
    for (let i = s.length - 1; i>= 0; i--) {
        if (continueAdding) {
            let num = parseInt(s[i], 10) + 1;
            if (num < 10) {
                newNumber += num;
                continueAdding = false;
            } else {
                newNumber += '0';
            }
        } else {  
            newNumber +=s[i];
        }
    }
    return newNumber.split("").reverse().join("");
}

Now, using the same example above

addOne('800000000000000000') + 1 = '800000000000000001'

Note that it must stay as a string or you will lose that 1 at the end.

Upvotes: 3

ericchan1336
ericchan1336

Reputation: 705

let pageId = '7'
pageId++
console.log(pageId)

Nowadays, you just need to pageID++.

Upvotes: 1

Oleg V. Volkov
Oleg V. Volkov

Reputation: 22421

+ happens to be valid operator for both strings and numbers that gives different results when both arguments are numeric and when at least one is not. One of possible workarounds is to use operator that only have numeric context but gives same mathematical result, like -. some_var - -1 will always be same as adding 1 to some_var's numeric value, no matter if it is string or not.

$('#arrowRight').attr('href', 'page.html?='+ (pageID - -1));

Upvotes: 5

Brett Zamir
Brett Zamir

Reputation: 14345

As long as your pageID is numeric, this should be sufficient:

$('#arrowRight').attr('href', 'page.html?='+(pageID+1));

The problem you were seeing is that JavaScript normally executes in left-to-right order, so the string on the left causes the + to be seen as a concatenator, so it adds the 7 to the string, and then adds 1 to the string including 7.

Upvotes: 0

Shikiryu
Shikiryu

Reputation: 10219

Simply, $('#arrowRight').attr('href', 'page.html?='+(pageID+1));

The parentheses makes the calculation done first before string concatenation.

Upvotes: 1

ninja
ninja

Reputation: 2263

It needs to be a integer, not a string. Try this:

pageID = parseInt(pageID)+1;

Then you can do

$('#arrowRight').attr('href', 'page.html?='+pageID);

Upvotes: 3

antyrat
antyrat

Reputation: 27765

Try this:

parseInt(pageID, 10) + 1

Accordint to your code:

$('#arrowRight').attr('href', 'page.html?='+ (parseInt(pageID, 10) + 1));

Upvotes: 37

Related Questions