Getting IE6 to auto-select an item in a dropdown [jQuery]

M

Mr. Wilson

On this page (just a demo) I can’t get IE6 to auto-select the first item
in the dynamically-generated drop-down menus. Well, that isn’t entirely
true. Sometimes it works properly, but never on the first try. When it
fails, the error message it gives is:

"Cannot set the selected property. Unspecified error."

FF2, IE7, and OP9 all behave as expected. I have tried a variety of
workarounds to no avail. I don't have an exhaustive list of everything I
have tried, but it seems like I have tried a hundred ways of setting the
"selected" attribute.

The page uses jQuery 1.1.2. Currently the page is using...

$("#select-how option:first-child").attr("selected","selected");

....to auto-select the first item in the list.

Can anybody offer me some clues?

--Mr. Wilson
 
R

RobG

On this page (just a demo) I can't get IE6 to auto-select the first item
in the dynamically-generated drop-down menus. Well, that isn't entirely
true. Sometimes it works properly, but never on the first try. When it
fails, the error message it gives is:

"Cannot set the selected property. Unspecified error."

Set the selected and defaultSelected attributes of the first option
when you create the list. No further action required.
 
L

-Lost

Mr. Wilson said:
On this page (just a demo) I can’t get IE6 to auto-select the first item in the
dynamically-generated drop-down menus. Well, that isn’t entirely true. Sometimes it
works properly, but never on the first try. When it fails, the error message it gives
is:

"Cannot set the selected property. Unspecified error."

FF2, IE7, and OP9 all behave as expected. I have tried a variety of workarounds to no
avail. I don't have an exhaustive list of everything I have tried, but it seems like I
have tried a hundred ways of setting the "selected" attribute.

The page uses jQuery 1.1.2. Currently the page is using...

$("#select-how option:first-child").attr("selected","selected");

...to auto-select the first item in the list.

Can anybody offer me some clues?

You do realize the default selection in most cases is the first element, therefore the
first-child?

I tested last-child, for example, in Internet Explorer 6 using jQuery 1.1.2 with no issue
or complaint. The last option of my select list was chosen by default.

-Lost
 
R

RobG

You do realize the default selection in most cases is the first element,

In practice, yes. But it is only convention to do so - the HTML
specification doesn't reqire it. It is good practice (though rarely
done) to ensure one option has the 'selected' attribute set.

If no option has its selected attribute set, Safari will not reset it
when the form's reset method is called either by script or using a
reset button.

therefore the first-child?

In some browsers, a select's firstChild might be a text node. I'm no
expert on jQuery, but it seems to skip text nodes when searching for
decendants - which might be a good thing sometimes but not others.

Responses here should focus on generic javascript and DOM, not a
particular implementation or library.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,001
Messages
2,570,251
Members
46,851
Latest member
CristineKo

Latest Threads

Top