20

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?

1
  • 3
    Almost a perfect example of that "how to add two numbers with jQuery" is no longer a joke! Jul 2, 2012 at 14:02

8 Answers 8

36

Try this:

parseInt(pageID, 10) + 1

Accordint to your code:

$('#arrowRight').attr('href', 'page.html?='+ (parseInt(pageID, 10) + 1));
3
  • Yes but in context: 'page.html?' + (parseInt(pageID, 10) + 1) - the subexpression has to be parenthesized.
    – Pointy
    Jul 2, 2012 at 13:52
  • parseInt is not required if pageID is already an integer, which it is in OP's question. You don't have to call parseInt on an number just to increment it Jul 2, 2012 at 15:28
  • 1
    Just wanted to point out that this solution does not work with strings that are larger than the size of an integer, So parseInt('800000000000000000', 10) + 1 = 800000000000000000, which is not what you want.
    – sookool99
    May 30, 2017 at 17:29
5

+ 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));
3

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.

2

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);
2
  • Make sure you pass the second parameter to parseInt (10) so that values like "08" and "09" work properly.
    – Pointy
    Jul 2, 2012 at 13:53
  • Very true, usually don't work with values that begin with '0' so out of habit I left it out. Nice catch!
    – ninja
    Jul 2, 2012 at 13:56
1

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

The parentheses makes the calculation done first before string concatenation.

0
1

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

Nowadays, you just need to pageID++.

1

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

0
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.