Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to preselect an item using Javascript when the page renders
    primarykey
    data
    text
    <p>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. </p> <pre><code>&lt;select class="form-select required" name="submitted[meal]" id="edit-submitted-meal"&gt; &lt;option selected="selected" value="1"&gt;Lunch&lt;/option&gt; &lt;option value="2"&gt;Dinner&lt;/option&gt; &lt;option value="3"&gt;Sunday Dining&lt;/option&gt; &lt;/select&gt; </code></pre> <p>I created the following JS snippet hoping to achieve the objective but it doesn't seem to work on Page load</p> <pre><code>window.onload() { var today = new Date("&lt;?php echo date("Y-m-d H:i:s"); ?&gt;"); 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 &gt; 15 &amp;&amp; hour &lt; 22) { meallist.options[2].selected==true; } else if (hour &gt; 22 &amp;&amp; hour &lt; 24 { meallist.options[1].selected==true; } else if (hour &gt; 0 &amp;&amp; hour &lt; 15 { meallist.options[1].selected==true; } } } </code></pre> <p>Would appreciate any help. Thank you in advance.</p> <p>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");</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload