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"?
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”).
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);
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).
$('select[name=name] option:eq(2)').attr('selected', 'selected');
Demo.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With