How do I use JavaScript in my & lt; Select & gt; List of options

advertisements

How do I use

          <select>
          <option>numbers1-100</option>
          </select>

I have to use 1 to 100 numbers in my option list.Typing all the options takes time and makes code bigger.I guess we have to use javascript to make it work using for loop and document write.But I dont know how to put the code in the right way.How do i list the options list using java script? I mean the java script should be beside my label;example

        number : 1-1oo \\ here the options list should be printed

and number can be anywhere but the script should print options list beside the number.How do i make it ? Unable to figure it out.Been trying from an hour or so.


Place this function in your <script> tags or include it within a script. Than call the function, createSelectOption() whenever you need the select box to be created.

Here is how you would have it loaded on page load with just javascript: `

function createSelectOption() {
    var select_option = '<select>';
    for(i = 1; i <= 100; i++) {
       select_option += '<option value=' + i + '>' + i + '</option>';
    }
    select_option += '</select>';
    document.getElementById('div').innerHTML = select_option;
}

I have included a jsfiddle demo to show you that it should be working/