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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:26:06+00:00 2026-05-27T20:26:06+00:00

I want this javascript to create options from 12 to 100 in a select

  • 0

I want this javascript to create options from 12 to 100 in a select with id=”mainSelect”, because I do not want to create all of the option tags manually. Can you give me some pointers? Thanks

function selectOptionCreate() {

  var age = 88;
  line = "";
  for (var i = 0; i < 90; i++) {
    line += "<option>";
    line += age + i;
    line += "</option>";
  }

  return line;
}
  • 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-27T20:26:07+00:00Added an answer on May 27, 2026 at 8:26 pm

    You could achieve this with a simple for loop:

    var min = 12,
        max = 100,
        select = document.getElementById('selectElementId');
    
    for (var i = min; i<=max; i++){
        var opt = document.createElement('option');
        opt.value = i;
        opt.innerHTML = i;
        select.appendChild(opt);
    }
    

    JS Fiddle demo.

    JS Perf comparison of both mine and Sime Vidas’ answer, run because I thought his looked a little more understandable/intuitive than mine and I wondered how that would translate into implementation. According to Chromium 14/Ubuntu 11.04 mine is somewhat faster, other browsers/platforms are likely to have differing results though.


    Edited in response to comment from OP:

    [How] do [I] apply this to more than one element?

    function populateSelect(target, min, max){
        if (!target){
            return false;
        }
        else {
            var min = min || 0,
                max = max || min + 100;
    
            select = document.getElementById(target);
    
            for (var i = min; i<=max; i++){
                var opt = document.createElement('option');
                opt.value = i;
                opt.innerHTML = i;
                select.appendChild(opt);
            }
        }
    }
    // calling the function with all three values:
    populateSelect('selectElementId',12,100);
    
    // calling the function with only the 'id' ('min' and 'max' are set to defaults):
    populateSelect('anotherSelect');
    
    // calling the function with the 'id' and the 'min' (the 'max' is set to default):
    populateSelect('moreSelects', 50);
    

    JS Fiddle demo.

    And, finally (after quite a delay…), an approach extending the prototype of the HTMLSelectElement in order to chain the populate() function, as a method, to the DOM node:

    HTMLSelectElement.prototype.populate = function (opts) {
        var settings = {};
    
        settings.min = 0;
        settings.max = settings.min + 100;
    
        for (var userOpt in opts) {
            if (opts.hasOwnProperty(userOpt)) {
                settings[userOpt] = opts[userOpt];
            }
        }
    
        for (var i = settings.min; i <= settings.max; i++) {
            this.appendChild(new Option(i, i));
        }
    };
    
    document.getElementById('selectElementId').populate({
        'min': 12,
        'max': 40
    });
    

    JS Fiddle demo.

    References:

    • node.appendChild().
    • document.getElementById().
    • element.innerHTML.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure how I would do this in Javascript. I want to create
I want to create a simple bookmarklet to pass some information from javascript to
i have this javascript code to which i want to add the jquery fade-in
I already started this in javascript so I don't want to use jquery but
I want to implement my own clustering algorithm using this Virtual Earth javascript API:
I want to define a JavaScript class, Foo. Foo = function(value){ this.value = value;
I want to protect my website against spambots with javascript. I found this code,
I'm using this HTML,CSS and Javascript code (in one document together if you want
I want to make a variable length array in Javascript. Is this possible. A
I am stuck with this issue. What I want to do is to create

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.