Markus
Markus

Reputation: 4038

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);

Upvotes: 0

Views: 5064

Answers (4)

wojtek
wojtek

Reputation: 11

This soulution is working fine under IE8 - copied from MIcrosoft forum -

"I assume you already got the answer you needed, but for anyone else who finds this post when searching this problem (like I did), here's the solution that worked for me. All it took was setting the properties on opt AFTER adding it to the options collection. I also found that MSDN's page on the add function (for the options collection) explicitly states that for IE, the properties must be set after the option is added, but most of the examples I found online don't do it that way. I think your way may have worked in older versions of IE."

var opt = document.createElement('option');  
select.options.add(opt);       
opt.innerHTML = 'Foo';  
opt.value = 'Bar';  

Upvotes: 1

Tim Down
Tim Down

Reputation: 324727

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");

Upvotes: 6

fearofawhackplanet
fearofawhackplanet

Reputation: 53466

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);

Upvotes: 2

lonesomeday
lonesomeday

Reputation: 238115

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

s.add(select_option);

Upvotes: 0

Related Questions