[thelist] Some more simple JavaScript
Christian Heilmann
lists at onlinetools.org
Fri Dec 3 12:51:15 CST 2004
Chris Kavanagh wrote:
> Dear list,
>
> I tried to Google for this but it didn't work out. In this form I
> have a selectable list that contains all the months, like so:
>
> <select size="1" name="Month">
> <option value="01">Jan</option>
> <option value="02">Feb</option>
> <option value="03">Mar</option>
> <option value="04">Apr</option>
> <option value="05">May</option>
> <option value="06">Jun</option>
> <option value="07">Jul</option>
> <option value="08">Aug</option>
> <option value="09">Sep</option>
> <option value="10">Oct</option>
> <option value="11">Nov</option>
> <option value="12">Dec</option>
> </select>
>
> I'd like the current month to be initially selected, and I need to use
> JavaScript to do it. I managed to figure out how to get the current
> month by virtue of some JavaScript:
>
> <SCRIPT language="JavaScript">
> var now = new Date();
> var thisYear = now.getYear();
> var thisMonth = now.getMonth();
> var thisHour = now.getHours();
> var thisMinute = now.getMinutes();
> </SCRIPT>
>
> But where do I go from here? I guess I need to put something like
> this in each <option> tag:
>
> if thisYear = 1, then "selected"
>
> Is this doable? Sorry I'm such a Java noob. It's all kinda alien to
> me, but I am trying.
Yes, but there is a much easier way: getMonth() returns the current
month starting from 0 and ranging up to 11. Every select has an array
of its options and the selectedIndex value defines which one is
selected. Put both together and you are done:
http://www.zen32721.zen.co.uk/monthselect.html
HTH
Chris
More information about the thelist
mailing list