Reputation: 2830
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
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
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