Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A selector to get the middle option-element by value is</p> <pre><code>$('.selDiv option[value="SEL1"]') </code></pre> <p>For an index:</p> <pre><code>$('.selDiv option:eq(1)') </code></pre> <p>For a known text:</p> <pre><code>$('.selDiv option:contains("Selection 1")') </code></pre> <p><strong>EDIT</strong>: 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:</p> <pre><code>$('.selDiv option:eq(1)').prop('selected', true) </code></pre> <p>In older versions:</p> <pre><code>$('.selDiv option:eq(1)').attr('selected', 'selected') </code></pre> <p><strong>EDIT2</strong>: after Ryan's comment. A match on "Selection 10" might be unwanted. I found no <a href="http://api.jquery.com/category/selectors/" rel="noreferrer">selector</a> to match the full text, but a <a href="http://api.jquery.com/filter/" rel="noreferrer">filter</a> works:</p> <pre><code> $('.selDiv option') .filter(function(i, e) { return $(e).text() == "Selection 1"}) </code></pre> <p><strong>EDIT3</strong>: Use caution with <code>$(e).text()</code> as it can contain a newline making the comparison fail. This happens when the options are implicitly closed (no <code>&lt;/option&gt;</code> tag):</p> <pre><code>&lt;select ...&gt; &lt;option value="1"&gt;Selection 1 &lt;option value="2'&gt;Selection 2 : </code></pre> <p>If you simply use <code>e.text</code> any extra whitespace like the trailing newline will be removed, making the comparison more robust.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload