Note that there are some explanatory texts on larger screens.

plurals
  1. POjQuery get parent optgroup
    primarykey
    data
    text
    <p>I'd like to detect if the selection option is under the 'international' optgroup. Right now my approach is to detect the closest optgroup from the selected item, and get the contents of its label attribute. I could test that string for the word 'international'. Other approaches are more then welcome. </p> <p>As you can tell, from this markup its unfortunate that the optgroup doesn't wrap the child options:</p> <pre><code>&lt;select id="venue"&gt; &lt;optgroup label="New England"&gt;&lt;/optgroup&gt; &lt;option value="1"&gt;&amp;nbsp;NH&lt;/option&gt; &lt;option value="2"&gt;&amp;nbsp;ME&lt;/option&gt; &lt;option value="3"&gt;&amp;nbsp;VT&lt;/option&gt; &lt;option value="4"&gt;&amp;nbsp;MA&lt;/option&gt; &lt;option value="4"&gt;&amp;nbsp;CT&lt;/option&gt; &lt;option value="4"&gt;&amp;nbsp;RI&lt;/option&gt; &lt;optgroup label="International"&gt;&lt;/optgroup&gt; &lt;option value="100"&gt;Canada&lt;/option&gt; &lt;option value="100"&gt;Texas&lt;/option&gt; &lt;option value="100"&gt;Mexico&lt;/option&gt; &lt;/select&gt; </code></pre> <p>The jQuery I've been working with obviously wont work, but you can see where I am trying to get to:</p> <pre><code>dropdownval = jQuery('select#venue option:selected') .parentsUntil( jQuery('optgroup'), '[label*="international"]') .attr('label');` </code></pre> <p><strong>EDIT</strong></p> <p>Its been pointed out to me that the following nesting of optgroups is also possible</p> <pre><code>&lt;select id="venue"&gt; &lt;optgroup label="New England"&gt;&lt;/optgroup&gt; &lt;option value="1"&gt;&amp;nbsp;NH&lt;/option&gt; &lt;option value="2"&gt;&amp;nbsp;ME&lt;/option&gt; &lt;option value="3"&gt;&amp;nbsp;VT&lt;/option&gt; &lt;option value="4"&gt;&amp;nbsp;MA&lt;/option&gt; &lt;option value="4"&gt;&amp;nbsp;CT&lt;/option&gt; &lt;option value="4"&gt;&amp;nbsp;RI&lt;/option&gt; &lt;optgroup label="International"&gt;&lt;/optgroup&gt; &lt;option value="100"&gt;&amp;nbsp;Canada&lt;/option&gt; &lt;option value="100"&gt;&amp;nbsp;Texas&lt;/option&gt; &lt;option value="100"&gt;&amp;nbsp;Mexico&lt;/option&gt; &lt;optgroup label="&amp;nbsp;Europe"&gt;&lt;/optgroup&gt; &lt;option value="100"&gt;&amp;nbsp;&amp;nbsp;Bruges&lt;/option&gt; &lt;/select&gt; </code></pre>
    singulars
    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.
 

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