I would like to know the best approach to use JavaScript to create a new HTML element <option> from each item in an array (or perhaps not use an array if there's a better method?
How would I run a loop that creates a new option for each item in the array and then assign the url as the options value.
My JS is as follows:
var images = [
"Sports 1",
"http://lorempixel.com/50/50/sports/1",
"Sports 2",
"http://lorempixel.com/50/50/sports/2",
"Sports 3",
"http://lorempixel.com/50/50/sports/3",
];
var index, len;
for (index = 0, len = images.length; index < len; index++) {
var newoption = document.createElement('option');
newoption.innerHTML = images[index];
document.getElementById('imagelist').appendChild(newoption);
}
The html should look like this:
<form id="imageform">
<select id="imagelist" name="imagelist">
<option value="http://lorempixel.com/50/50/1">Sports 1</option>
<option value="http://lorempixel.com/50/50/1">Sports 1</option>
<option value="http://lorempixel.com/50/50/1">Sports 1</option>
</select>
</form>