Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I select the next element that contains certain text?
    primarykey
    data
    text
    <p>I am trying to select the next <code>&lt;option&gt;</code> that contains certain text. </p> <p>Here is my scenario:</p> <pre><code>&lt;select id="stock"&gt; &lt;option&gt;Blue&lt;/option&gt; &lt;option&gt;-Small&lt;/option&gt; &lt;option&gt;-Medium&lt;/option&gt; &lt;option&gt;-Large&lt;/option&gt; &lt;option&gt;Black&lt;/option&gt; &lt;option&gt;-Small&lt;/option&gt; &lt;option&gt;-Medium&lt;/option&gt; &lt;option&gt;-Large&lt;/option&gt; &lt;option&gt;Red&lt;/option&gt; &lt;option&gt;-Small&lt;/option&gt; &lt;option&gt;-Medium&lt;/option&gt; &lt;option&gt;-Large&lt;/option&gt; &lt;/select&gt; </code></pre> <p>I can easily find the color by using this:</p> <pre><code>$('#stock').find('option:contains('Black')').attr('selected','selected'); </code></pre> <p>But then I would like to select the size that is directly after that color. I have tried using next() like this:</p> <pre><code>$('#stock').find('option:contains('Black')').next('option:contains('Medium')').attr('selected','selected'); </code></pre> <p>But apparently, next() only select the element immediately after. So, I tried nextAll() but it selects both Mediums after Black. So, I then tried using first() after nextAll, but that didn't work either. Nothing selected.</p> <pre><code>$('#stock').find('option:contains('Black')').nextAll('option:contains('Medium')').first().attr('selected','selected'); </code></pre> <p>Am I missing something obvious? Thanks for your help!</p> <p><strong>Update:</strong> I have it working now adding the :first selector to nextAll(). Thanks for your comments.</p> <pre><code>$('#stock').find("option:contains('Black')").nextAll("option:contains('Medium'):first").attr('selected','selected'); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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