Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9138667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:17:12+00:00 2026-06-17T09:17:12+00:00

I have a drupal rendered webform that generates the following HTML for a SELECT

  • 0

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”);

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T09:17:13+00:00Added an answer on June 17, 2026 at 9:17 am

    You have an error in your javascript syntax

       window.onload(){
          //your code here
       }
    

    should be

       window.onload = (function(){ 
          //your code here
        });
    

    Also when using document.getElementById you don’t want to include the ‘#’

    and for setting a variable as in

     meallist.options[1].selected==true;
    

    you only want to use a single =

      meallist.options[1].selected=true;
    

    Also bear in mind that the options array is 0 based. E.g options value=3 is actually

      meallist.options[2] not [3]
    

    A simplified version can be found here
    http://jsfiddle.net/N7Yhr/

    Do you have any other errors in your console window (firebug etc)?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a list rendered by drupal(7)'s item-list theme hook, the output becomes something
I have Drupal 6 website which is using FCK Editor as HTML editor. It
I have a Drupal form array that uses a prefix of '#' to indicate
I have a Drupal view that displays vacation leaves for a particular employee. I
I have a Drupal setup like this: Content type: Apartments Vocabulary: Areas , that
I have a Drupal 7 installation that shows the default front page. Published pages/nodes
We have a intranet site that runs on Drupal. If an employee hits the
I have a Drupal website that has a page at the URL alias of
I have a drupal database where the node table is full of profiles. The
I have custom drupal module. I receive result from a webservice that I need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.