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 6041549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:40:39+00:00 2026-05-23T06:40:39+00:00

I have written some javascript code that’s supposed to put options into a select

  • 0

I have written some javascript code that’s supposed to put options into a select box. The select box’s code is below:

<select id="dayOfTheWeek" name="dayOfTheWeek"></select>

In my javascript coding, I have a method called populateDays that’s supposed to fill the select box with 7 options – one for each day of the week. The code for it is below:

function populateDays() {
var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
var dayOfWeek = document.getElementById("dayOfTheWeek");
var today = (new Date()).getDay();

for (var i = 0; i < days.length; i++) {
    var length = dayOfWeek.options.length;
    dayOfWeek.add(new Option(days[i], i), length);
}
dayOfWeek.selectedIndex = today;
}

Later on in the script I make a call to this method. I know that the browser runs the javascript code when the browser loads because I put an alert() statement inside of the populateDays method and it appeared as I loaded the page.

populateDays();

For some reason, when I load the page, the select box is completely empty. Why does the populateDays() method not load that select box with the options?

  • 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-05-23T06:40:40+00:00Added an answer on May 23, 2026 at 6:40 am

    Erm, it should work just fine. If it doesn’t:

    Method 2 (jsFiddle example) – innerHTML

    function populateDays() {
      var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
      var dayOfWeek = document.getElementById("dayOfTheWeek");
      var today = (new Date()).getDay();
      var options = "";
    
      for (var i = 0; i < days.length; i++) {
        options += '<option value="' + days[i] + '">' + days[i] + '</option>'
      }
    
      dayOfWeek.innerHTML = options;
      dayOfWeek.selectedIndex = today;
    }
    

    Method 3 – creating elements programmatically

    function populateDays() {
      var days = [ "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" ];
      var dayOfWeek = document.getElementById("dayOfTheWeek");
      var today = (new Date()).getDay();
    
      for (var i = 0; i < days.length; i++) {
        var option = document.createElement("option");
        option.value = days[i];
        var optionText = document.createTextNode(days[i]);
        option.appendChild(optionText);
        dayOfWeek.appendChild(option);
      }
    
      dayOfWeek.selectedIndex = today;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some Javascript code that will programmatically register an COM interop assembly by
I have written some JavaScript code. It adds new function to NodeList type in
Ive got a really strange problem with some javascript code that ive written. I
While debugging javascript written by someone else, I came across some code that I've
I have an asp.net web page written in C#. Using some javascript I popup
I have written some code in my VB.NET application to send an HTML e-mail
I'm just getting into unit testing, and have written some short tests to check
I have some written a number of unit tests that test a wrapper around
I have some source files that have comments written in Japanese. When I open
Suppose I have a master page where I have written some javascript to access

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.