I have this function for adding options to a Select list:
function addOption(selectbox, value, text, cl )
{
var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
if (cl==1){ optn.className = "nav_option_main"; }
selectbox.options.add(optn);
}
I have just discovered the “optgroup” tag, but I don’t know how to implement it to the drop list.
The function above is called on another selectboxes “OnChange” event, to fill sub-select-box with the desired options.
I don’t think it should be too difficult to add the “optgroup” tag into this, or is it?
Thanks and if you need more input let me know…
UPDATE:
When triggering your function Beejamin the optgroup label is copied beneath one another.
Ex:
Label
Label
Label
optgroup.1
optgroup.2
optgroup.3
etc...
Thanks for the function though… But how can I fix this?
Here’s an example:
http://www.spookandpuff.com/examples/optGroup.html
And here’s the function – which is adapted from your original one:
This version accepts a parameter ‘contents’, rather than a separate params for ‘text’ and ‘value’ – this accepts an array of one or more options. So, to add a single option, not as part of a group, do this:
The item in the contents array is itself an array of two items – the text, and the value.
If you stack more than one of these into the contents array, you create an optGroup:
The first item is the title for the group, after that, it’s just repeating the same content as the single item. When the function detects you’re trying to add more than one at a time, it groups them together.
Is that what you’re after?