Jump to content

Passing arrays between php files

Recommended Posts

Just one last question a little bit offtopic. I have this two lines of code:

month = document.calForm.selMonth.options[document.calForm.selMonth.selectedIndex].value;month += 1;

What I expected that should happen was the variable "month" to be incremented, but what really happen is that if "month" is equal 5 on the end of the first line then in the second line it will result "51" . The instruction "month += 1" does not do a numerical calculation, instead, it joins "1" to the previous value of the variable. How can I change this?

Link to post
Share on other sites

Unfortunately, the + operator works on strings and numbers differently. In your case, it thinks it's operating on strings, because the value of a form control is always a string. Try this:

month = parseInt(month) + 1;

There are tricks you can use to save a few characters in that code, but this way makes your intentions 100% clear.

Edited by Deirdre's Dad
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...