Im trying to populate a dropmenu as described in the heading. At the moment I've this code:
<form id="myForm">
<select id="selectNumber">
<option>Choose a number</option>
<script>
var myArray = new Array("1", "2", "3", "4", "5");
for(i=0; i<myArray.length; i++) {
document.write('<option value="' + myArray[i] +'">' + myArray[i] + '</option>');
}
</script>
</select>
</form>
This works fine, but I have a large amount of variables in my array, so i want to put the script in a javascript file as a function and then call it in the html for aesthetic purposes.
So this is what i tried in the javascript file
function populate(){
var myArray = new Array("1", "2", "3", "4", "5");
for(i=0; i<myArray.length; i++) {
document.write('<option value="' + myArray[i] +'">' + myArray[i] + '</option>');
}
}
And then I tried to call that method in the HTML like so :
<form id="myForm">
<select id="selectNumber" onclick="populate()">
<option>Choose a number</option>
</select>
</form>
and this didn't work it just brought a new page and the numbers , but not in a dropdown menu to select from.
Any ideas on how to fix it would be much appreciated
document.writeat all. It's used for very specific purposes and not often. You need document.createElement, appendChild in this case.