[snip]
("snip" explained here said:
Sure. I'd be especially interested in its faults.
Invocation is via an onChange event
<snip>
<select name="month" id="month" size="1"
onchange="setDayBox(document.Reservation.month.options[document.Reservation.month.selectedIndex].value);">
Accessing form controls the nonstandard way, as you have, has known side
effects. The most obvious is that if the control is orphaned from the
document after it has been accessed off the form, it remains as a
property of the form, even if there are no other references pointing to it.
To correctly access a form control, please see:
/* ******************************************************** */
/* setDayBox */
/* Change the number of days in the Day drop down box to */
/* reflect the number of days in the selected month. */
/* Input argument: */
/* chosen: Selected month (1 to 12) */
/* ******************************************************** */
function setDayBox(chosen) {
var numberOfDays = selectNumberOfDaysInMonth(parseInt(chosen));
It is a good idea to be in the habit of using a radix with parseInt.
var dayBox = document.Reservation.Day;
dayBox.options.length = 0;
for(i=0; i < numberOfDays; i++) {
Don't forget var.
dayBox.options[dayBox.options.length] = new Option(i
+1,i,false,false);
The strategy employed sets dayBox to numberOfDays each time a month is
selected. That is not a very efficient approach.
Instead, when the selected month contains less than 31 days, why not
disable the days from the end of the month. That way a js-disabled user
(such as myself), has the ability to use it.
Rough outline:
// untested.
function adjustDaysForDate(selectedMonth, selectedYear) {
var daysInMonth = getDaysInMonth(selectedMonth, selectedYear);
var disabledDays = 31 - daysInMonth;
if(disabledDays) {
disableInvalidDays(disabledDays);
}
}
function disableInvalidDays(count) {
for(var i = 0; i < count; i++) {
daySelectElement.options[30-i].disabled = true;
}
}
function getDaysInMonth(month, year) {
// etc.}
[snip]
[snip Stockton's sig]