I'm trying to fill an html select with javascript. Now I simply do:
function addRow(selectID, testName) {
var x = document.getElementById(selectID);
var option = document.createElement("option");
option.text = testName;
x.add(option);
}
for (var x = 0; x < testSet.length; x++) {
addRow('select', testSet[x]);
}
where testSet is an array of string. In this way I have the filled select. Now I want to upgrade the fill of select using optgroup so, in my html code, I have
<select onchange="selectionChanged()"
style="width: 400px; height: 20px; vertical-align: middle;"
id="select"><optgroup label="OptionSet1"></optgroup>
<optgroup label="OptionSet2"></optgroup>
<optgroup label="OptionSet3"></optgroup>
</select>
Now to fill with my options I think I must do something like that
for (var x = 0; x < testSet.length; x++) {
if(testSet[x].indexOf("string1") != -1){
// Add testSet[x] in OptionSet1
}
if(testSet[x].indexOf("string2") != -1){
// Add testSet[x] in OptionSet2
}
if(testSet[x].indexOf("string3") != -1){
// Add testSet[x] in OptionSet3
}
}
What I must do to "Add testSet[x] in OptionSet"? I readed something on the web, but I cannot find anything that seems to work for me.
Thank you.
Maybe this give you little idea
on how to do it
You can improve the code to make it more meaning full to your usage
thanks