0

in this var: var previousmenu = $("#ipadmenu section.current").data('order') + 1;, this expression $("#ipadmenu section.current").data('order') equals 0.

Unfortunately the result of this is 01 and not 1. Why is this?

This for example seems to work: var previousmenu = $("#ipadmenu section.current").data('order') - 1; and gives me -1.

Thank you for your help.

0

3 Answers 3

3

+ serves two purposes. It adds in the case of numbers and concatenates in the case of strings. You can fix this by casting the string as a number with this:

var previousmenu = Number($("#ipadmenu section.current").data('order')) + 1;
Sign up to request clarification or add additional context in comments.

Comments

1

If one the variables you are 'adding' is a string, javascript decides that what you really wanted to do was concatenate the string and the number together (hence "0" + 1 = "01");

What you need to do is something like..

var order = parseInt($("#ipadmenu section.current").data('order'), 10);
order = order + 1;

Comments

0

Because it's a string, not an integer, thus doing string concatenation... aaand welcome to the whacky world of JavaScript, because you guessed it, "0" - 1 will return a number.

In any case, use parseInt(...) or + $("blah").data('order') + 1 and you'll be happier, if not confused.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.