I have a drupal rendered webform that generates the following HTML for a SELECT list. The list is essentially for booking a table in a restaurant. My client wants me to preselect the meal based on the time of the day. So if its between midnight and 3:00 pm Lunch should be preselected automatically. After 3:00 pm till 10:30pm the form should display with dinner preselected.
<select class="form-select required" name="submitted[meal]" id="edit-submitted-meal">
<option selected="selected" value="1">Lunch</option>
<option value="2">Dinner</option>
<option value="3">Sunday Dining</option>
</select>
I created the following JS snippet hoping to achieve the objective but it doesn’t seem to work on Page load
window.onload() {
var today = new Date("<?php echo date("Y-m-d H:i:s"); ?>");
var day = date.getDay();
var hour = date.getHours();
var meallist = document.getElementbyId("#edit-submitted-meal");
if (day == 0) {
meallist.options[3].selected==true;
}
else {
if (hour > 15 && hour < 22) {
meallist.options[2].selected==true;
}
else if (hour > 22 && hour < 24 {
meallist.options[1].selected==true;
}
else if (hour > 0 && hour < 15 {
meallist.options[1].selected==true;
}
}
}
Would appreciate any help. Thank you in advance.
PS : The PHP code injects the date into the javascript so when the page is rendered the line becomes var today = new Date(“2013-01-15 15:49:45”);
You have an error in your javascript syntax
should be
Also when using document.getElementById you don’t want to include the ‘#’
and for setting a variable as in
you only want to use a single =
Also bear in mind that the options array is 0 based. E.g options value=3 is actually
A simplified version can be found here
http://jsfiddle.net/N7Yhr/
Do you have any other errors in your console window (firebug etc)?