PDA

View Full Version : Add 1 hour to drop down menu?



auntnana
02-01-2010, 06:50 PM
I have calendars on my site where tenants can make reservations for shared rooms. The start times are listed in a drop down starting at 8am and going through 5pm at one hour intervals. What I'd like to be able to do is make the drop down be populated 1 hour from the current time. For example, if it is 8 now, I'd like the time in the drop down to start at 9. Is this something that's possible or wishful thinking?

Old Pedant
02-01-2010, 08:15 PM
<script>
function addReservationTimes( sel )
{
var now = new Date();
for( var hr = now.getHour() + 1; hr < 21; ++hr )
{
var show = hr;
var ampm = ":00 AM";
if ( show > 11 ) ampm = ":00 PM";
if ( show > 12 ) show -= 12;
show += ampm;

sel.options[sel.options.length] = new Option( show, show );
}
}
</script>
...
<body onload="addReservationTimes( document.ResForm.ReservationHour );">
...
<form name="ResForm" ...>
...
<select name="ReservationHour">
<option value="">--please choose an hour--</option>
</select>
...
</form>


Untested. Off the top of my head. But can't see why it wouldn't work.

Note: This will start at 10:00 AM even if the current time is 9:00:01. If you'd like a little flexibility--say let the tenant reserve the current hour even up to 15 minutes after the hour starts, that's easy enough to do.

Oh, and that arbitrarily stops the times at 9PM (the 21 in there, 2100 hours). Change the 21 to change that.