Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use JQuery to select the nth option

I have the following HTML

<select onchange="this.form.submit()" name="name">     <option value="9995101E01#17201044055PM">9995101E01#17201044055PM</option>     <option value="GRR-Example">GRR-Example</option>     <option value="admin">admin</option>     <option value="123w">123w</option> </select> 

May I know how I can use JQuery to option with value "admin"?

like image 604
Cheok Yan Cheng Avatar asked Nov 19 '10 07:11

Cheok Yan Cheng


People also ask

How do I select a specific Dropdownlist using jQuery?

Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).

How do I select a selected box using jQuery?

You can select on any attribute and its value by using the attribute selector [attributename=optionalvalue] , so in your case you can select the option and set the selected attribute. $("div. id_100 > select > option[value=" + value + "]"). prop("selected",true);

How do I get the first option selected in jQuery?

Select the <select> element using JQuery selector. This selector is more specific and selecting the first element using option:nth-child(1). This will get access to the first element (Index starts with 1).


2 Answers

$('select[name=name] option:eq(2)').attr('selected', 'selected'); 

Demo.

like image 174
Darin Dimitrov Avatar answered Sep 30 '22 22:09

Darin Dimitrov


Like this:

$("select[name='name'] option:eq(2)").attr("selected", "selected"); 

EDIT:

To do what you want in your new edit, that is, select the option that has the value of admin:

$("select[name='name'] option:contains('admin')").attr("selected", "selected"); 

See it working.

like image 36
Alex Avatar answered Sep 30 '22 21:09

Alex