I have a number that is generated dynamically, and I would like to add this number of option elements to a select. I am thinking a for loop would be the best way to do this. Say my variable containing the number is var number. thanks for any help.
My starting HTML is:
<select id="test">
</select>
Using Javascript, I need something like this:
function selectOne() {
var select = document.getElementById('test');
// for loop goes here?
// e.select[0] = new Option(number); // do this n times with value also equaling number
}
So my resulting HTML needs to look like this, if the number = 6:
<select id="test">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
This builds n options :
Demonstration