views:

206

answers:

2

I need a cross-platform way to insert OPTIONs into a SELECT with jQuery. I think I recall in the past that IE6 does nothing when this is called:

<select id="myselect" size="1">
<option value=""></option>
</select>
<script type="text/javascript">
$('#myselect').append('<option value="test1">test1</option>');
$('#myselect').append('<option value="test2">test2</option>');
</script>

I think I recall that the above worked in all browsers as well as Firefox 2+ and IE7+, but not IE6. Correct? If so, what's the workaround?

+1  A: 

What you recalled is wrong. What you are doing is right? Check this similar question on SO. And why is this a WIKI??

Teja Kantamneni
Your similar question has the wrong answer. Won't work in IE's, and others express so in the comments on the other answers. Also, once marked as a wiki, I can't edit it to make it non-wiki. My mistake, but can't fix.
Volomike
Teja Kantamneni
I can't get a copy of IE6 unfortunately to test against. Recalled I had this same problem last year and never resolved. Perhaps the problem existed in all IEs up to IE8.
Volomike
A: 

First off, you aren't waiting for the DOM to be ready with your code. You should be wrapping your jQuery code in:

$(document).ready(function() {

    $('#myselect').append('<option value="test1">test1</option>');
    $('#myselect').append('<option value="test2">test2</option>');

});

I'm not sure about IE6 compatibility, but you could try the .appendTo function instead, such as:

$('<option value="Test3">Test 3</option>').appendTo("#myselect");

example:

http://jsfiddle.net/W6L9d/

KP
Again, nobody is testing in IE6. I recall last year that IE6 had this bug. Unfortunately I don't have an IE6 workstation to test with anymore.
Volomike
Ok. Well I think if you're concerned about IE6 compatibility in general, you should find a way to test, personally. There are ways to install IE6 and 7 side by side (http://tredosoft.com/Multiple_IE for example).
KP
I have Ubuntu Linux as my development workstation. I have a test VM for Windows 2008 Server Trial Version that I use for testing everything else but IE6. Tredosoft's solution won't work on that.
Volomike