If you know the Index, Value or Text. also if you don't have an ID for a direct reference.
This, this and this are all helpful answers.
Example markup
<div class="selDiv">
<select class="opts">
<option selected value="DEFAULT">Default</option>
<option value="SEL1">Selection 1</option>
<option value="SEL2">Selection 2</option>
</select>
</div>
You could name the select and use this:
It should select the option you want.
try this new one
Exactly it will works
I use this, when i know the index of the list.
This allows the list to change, and fire the change event. The ":nth(n)" is counting from index 0
A selector to get the middle option-element by value is
For an index:
For a known text:
EDIT: As commented above the OP might have been after changing the selected item of the dropdown. In version 1.6 and higher the prop() method is recommended:
In older versions:
EDIT2: after Ryan's comment. A match on "Selection 10" might be unwanted. I found no selector to match the full text, but a filter works:
EDIT3: Use caution with
$(e).text()
as it can contain a newline making the comparison fail. This happens when the options are implicitly closed (no</option>
tag):If you simply use
e.text
any extra whitespace like the trailing newline will be removed, making the comparison more robust.None of the methods above provided the solution I needed so I figured I would provide what worked for me.