Ticket #4998 (closed bug: invalid)
attr("selected", true) fails in IE6
| Reported by: | scottamain | Owned by: | |
|---|---|---|---|
| Priority: | major | Milestone: | 1.4 |
| Component: | core | Version: | 1.3.2 |
| Keywords: | IE6, selected | Cc: | |
| Blocking: | Blocked by: |
Description
In my script, I'm finding an <option> element and setting it to be selected.
The following code fails in IE6:
var selectedItem = $("#Control option[value='"+selectedLevel+"']"); selectedItem.attr("selected","true");
This code, however, works in IE6 (and all browsers):
var selectedItem = $("#Control option[value='"+selectedLevel+"']").get(0); selectedItem.setAttribute('selected',true);
So the normal DOM function "setAttribute" is more successful than jQuery, which I suspect is using the (known-to-fail in IE6) "selected" field (as in, selectedItem.selected = true)... that fails in IE6 the same as my jquery sample above.
I'm using v1.3.2
Change History
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

You should be using:
selectedItem.attr("selected",true);That's always worked for me on all browsers. Since the option element has a selected property, it's used in preference to the html attribute when you try to set it with .attr().