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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:52:15+00:00 2026-06-03T02:52:15+00:00

I want to fill a dropdown list and I’ve written the following code: <script

  • 0

I want to fill a dropdown list and I’ve written the following code:

<script language="JavaScript1.2">
window.onload = fillDropDown();

function fillDropDown() {

        var ddl = document.getElementById("dia");
    var theOption = new Option;
    var x;
    var i;

    for(i = 1; i < 32; i++) {
        x = i + 1;
        theOption.text = x;
        theOption.value = x;
        ddl.options[i] = theOption;
    }


}

</script>
   <body>
    <form>
           <select id=dia></select>
    </form>
   <body>

It isn’t working, any idea why?

  • 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-03T02:52:16+00:00Added an answer on June 3, 2026 at 2:52 am

    You have some errors in your code.

    1. The first common mistake is the following:

    window.onload = fillDropDown();
    

    This previous code is registering the result of the called function fillDropDown and then assigning its results into window.onload. Thus, this will never do something. To register an event you have to assign a function not the result of a called function. The difference is this:

    window.onload = fillDropDown; // Without the parentheses.
    

    2. Another mistake I found, is about the creation of the option element. The better way to create HTML element in JavaScript is using the almost-standard document.createElement function.

    3. Also your HTML markup has an error. Your select is written: <select id=dia></dia> and it should be <select id="dia"></select>;

    So, with all theses changes highlighted the resulting code will be like this:

    window.onload = fillDropDown;
    
    function fillDropDown() {
    
        var ddl = document.getElementById("dia");
        var theOption;
        var x;
        var i;
    
        for (i = 1; i < 32; i++) {
            x = i + 1;
            theOption = document.createElement('option');
            theOption.label = x;
            theOption.value = x;
            ddl.add(theOption, null);
        }
    
    
    }​
    

    And it works like a charm. You can see it in action in this live demo: http://jsfiddle.net/dyjLS/

    Note: I highly recommend to use a JavaScript library such as jQuery to do this, since it will deal with almost all the cross-browser inconsistencies. If you use it, your code will look like the following:

    <script type="text/javascript">
      jQuery( document ).ready( function($) {
        var $select = $('#dia');
        for ( var i = 1 ; i < 32 ; i++) {
          var x = i + 1;
          $select.append('<option value='+ x +'>'+ x +'</option>');
        }
      });
    </script>
    

    ¡Voilà!

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

Sidebar

Related Questions

To dynamically fill DropDown controls on my HTML Form, I have written code that
I Had Drop down list and I want to fill it with data from
I want to add Drop Down List html control to the web page fill
I simply want to fill-up cells in my spreadsheet from a VBA function. By
I had drop down list and I want to fill it with data by
I want to fill dropdownlist which is inside FormView. I'm writing this code at
I have a page that contains dynamically generated Dropdown List controls and I want
I have a dropdown which i want to fill through jquery. Problem that i
I want to fill a table with a list of results. But not sure
I want to fill a map with class name and method, a unique identifier

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.