Search code examples
javascriptjqueryinternet-explorerhtml-select

Internet Explorer doesn't know how to add options tag to a select in jQuery


Im facing a problem with jQuery in the Internet Explorer 7 and 8, while trying to add a option to a existing select:

var s = document.getElementById("category");
s.options.add(select_option);

But IE just says:

Object doesn't support this property or method

and points to s.options.add(select_option);


Solution

  • Assuming the element with id "category" is actually a <select>, the easiest way is the the following time-honoured code for adding an option to a select list in any browser:

    var s = document.getElementById("category");
    s.options[s.options.length] = new Option("Option text", "optionValue");