Adam Brave Posted January 18, 2013 Author Share Posted January 18, 2013 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 comment Share on other sites More sharing options...
jeffman Posted January 18, 2013 Share Posted January 18, 2013 (edited) 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 January 18, 2013 by Deirdre's Dad Link to comment Share on other sites More sharing options...
Adam Brave Posted January 18, 2013 Author Share Posted January 18, 2013 Thanks, problem solved Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now