Click to See Complete Forum and Search --> : why is this concatenating 1 rather than adding 1?


kj2398
07-25-2003, 11:41 AM
I'm new to javascript and I'm writing a javascript function to grab the current month field from a form, add then add 1 to it. I was able to do this okay and subtract one from the current month. Heres the subtract code that works okay,

var cm = document.forms(0).elements.targetmonth.value;
var cy = document.forms(0).elements.targetyear.value;
cm = cm - 1;

if (cm == 0)
{
cm = 12
cy = cy - 1
}
else
{
}
so if the current month = 3, then this makes current month 2.

The add code:

alert("currmonth = " + document.forms(0).elements.targetmonth.value);
alert("curryear = " + document.forms(0).elements.targetyear.value);
var cm = document.forms(0).elements.targetmonth.value;
var cy = document.forms(0).elements.targetyear.value;
cm = cm + 1;
alert(cm);
if (cm == 13)
{
cm = 1;
cy = cy + 1;
}
else
{
}
alert("cm = " + cm + " cy = " + cy);

If the current month = 2 then this makes the current

pyro
07-25-2003, 11:46 AM
Make sure they are numbers, using either Number() (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/number.html#1193137) or parseInt (http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/toplev.html#1064173)

kj2398
07-25-2003, 11:55 AM
Thanks, I used the Number() and that worked.

pyro
07-25-2003, 11:59 AM
You're welcome... :)