views:

35

answers:

3

Hi there,

Im facing a problem with jquery in the Internet Explorer 7 and 8:

Im Trying to add a option tag 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 the s.options.add(select_option); line...

Anybody an idea? Thanks Maechi

A: 

Should the add() call not be on the select element, rather than the collection of options? I.e.:

s.add(select_option);
lonesomeday
That doesn't work unfortunately...
Markus
+2  A: 

try

$('#category').append('<option value="foo" selected="selected">Foo</option>');

or

var options = $('#category').attr('options');
options[options.length] = new Option('Foo', 'foo', true, true);
fearofawhackplanet
var options = $('#category').attr('options'); really?? that works?
Diego
Why **wouldn't** that work? `<select>` DOM elements always have an "options" attribute.
Pointy
@Diego: jQuery's `attr()` function actually maps to a property name where possible. It may be confusing, but `$(element).attr('options')` is the equivalent of `element.options`.
Andy E
select elements have tags "option" as inner html, didn't know it was also an attribute
Diego
+4  A: 

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");
Tim Down
+1, although there's no reason *element.options.add()* wouldn't work unless you were trying to migrate an option element from a different document, but that would give a different error.
Andy E
@Andy: Yes indeed, `s.options.add(new Option("text", "value"))` would work just as well. My version is what I used to use when coding for IE 4 and Netscape 4 (which didn't have the `add()` method). I think it worked in Netscape 3 too.
Tim Down