Search code examples
jqueryoptgroup

optgroup not viewable?


Why is the optgroup here not viewable?

All options are shown, but not the group name

http://jsfiddle.net/UCNJ8/

$('<select></select>').appendTo('body').append('<option value="0">test</option>')
    .append($('<optgroup>group</optgroup>')
            .append('<option value="1">group test</option>'));

Solution

  • The group name is set via the label attribute (HTML4 specification), not by its content (only option elements can be children of optgroup elements).

    $('<optgroup />', {label: "group"})
    

    DEMO