Alex Rivera | Logout

How to select the first element in the dropdown using jquery?

Asked 2009-09-08T15:09:02.213
116

I want to know how to select the first option in all select tags on my page using jquery.

tried this:

$('select option:nth(0)').attr("selected", "selected"); 

But didn't work

Edit
Report

1 Answer

4

What you want is probably:

$("select option:first-child")

What this code

attr("selected", "selected");

is doing is setting the "selected" attribute to "selected"

If you want the selected options, regardless of whether it is the first-child, the selector is:

$("select").children("[selected]")
answered 2009-09-08T15:15:41.763

Your Answer