I have an array of objects that I'd like to use to populate a Select option dropdown, but these elements do not already exist and must be created from scratch.
I have working code using the createElement method, but I can't use that method nor can I use options.add(new Option()). How else can I create the select and option elements in the JavaScript?
var optionsList = [
{
label: "Option 1",
value: "option-1"
},
{
label: "Option 2",
value: "option-2"
},
{
label: "Option 3",
value: "option-3"
}
];
if (optionsList.length == 0) {
console.log("No data");
} else {
var selectTag = document.createElement("SELECT");
document.body.appendChild(selectTag);
for (var i = 0; i < optionsList.length; i++) {
var option = optionsList[i];
selectTag.options.add(new Option(option.label, option.value));
}
}