Dave A
Dave A

Reputation: 2830

In jQuery, given a select menu element, how do I access the first option element?

Given a jQuery element like so:

$(selectElt)

How do I access the first option? Note I am not interested in a selector given the select menu's id, e.g.

$('#selectmenu1 option:first')

What I am given is the select element itself, $(selectElt), from which I need to select the first option element.

Upvotes: 2

Views: 90

Answers (3)

Y.Puzyrenko
Y.Puzyrenko

Reputation: 2195

You can try

$(selectElt).children('option').eq(0)

or

$(selectElt).children('option:first')

or

$(selectElt).children('option').first()

As RoryMcCrossan said

$('option:first',selectElt)

And the last one

$(selectElt).children('option').filter(':first')

Upvotes: 4

user821445
user821445

Reputation:

Alternatively:

$(selectElt).filter(":first")

Upvotes: 1

Mathew Thompson
Mathew Thompson

Reputation: 56459

Just do:

$(selectElt).children("option:first");

or

$(selectElt).children("option:eq(0)");

or

$(selectElt).children("option:nth-child(1)");

or contextually:

$("option:first", selectElt)

Upvotes: 2

Related Questions