Create an OptionGroup Element - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:OptionGroup


You can create an <optgroup> element by using the document.createElement() method:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<button onclick="myFunction()">create an OPTGROUP element</button>

<select id="mySelect" size="6">
</select>/*w w w.ja v a2  s.  c  o m*/

function myFunction() {
    var x = document.getElementById("mySelect");
    var y = document.createElement("OPTGROUP");
    y.setAttribute("label", "group1");
    x.insertBefore(y, x.options[3]);


Related Tutorials