Note that there are some explanatory texts on larger screens.

plurals
  1. POHiding <option>s in IE
    primarykey
    data
    text
    <p>I wrote this nifty function to filter select boxes when their value is changed...</p> <pre><code>$.fn.cascade = function() { var opts = this.children('option'); var rel = this.attr('rel'); $('[name='+rel+']').change(function() { var val = $(this).val(); var disp = opts.filter('[rel='+val+']'); opts.filter(':visible').hide(); disp.show(); if(!disp.filter(':selected').length) { disp.filter(':first').attr('selected','selected'); } }).trigger('change'); return this; } </code></pre> <p>It looks at the <code>rel</code> property, and if the element indicated by <code>rel</code> changes, then it filters the list to only show the options that have that value... for example, it works on HTML that looks like this:</p> <pre><code>&lt;select id="id-pickup_address-country" name="pickup_address-country"&gt; &lt;option selected="selected" value="CA"&gt;Canada &lt;/option&gt; &lt;option value="US"&gt;United States &lt;/option&gt; &lt;/select&gt; &lt;select id="id-pickup_address-province" rel="pickup_address-country" name="pickup_address-province"&gt; &lt;option rel="CA" value="AB"&gt;Alberta &lt;/option&gt; &lt;option selected="selected" rel="CA" value="BC"&gt;British Columbia &lt;/option&gt; &lt;option rel="CA" value="MB"&gt;Manitoba &lt;/option&gt;... &lt;/select&gt; </code></pre> <p>However, I just discovered it doesn't work properly in IE (of course!) which doesn't seem to allow you to hide <code>options</code>. How can I work around this?</p> <hr> <p>Here's what I've got now:</p> <pre><code>(function($) { $.fn.cascade = function() { var filteredSelect = $(this); var filteredOpts = this.children('option'); var triggerSelect = $('[name='+this.attr('rel')+']'); triggerSelect.change(function() { var triggerValue = $(this).val(); filteredOpts.detach() .filter('[rel='+triggerValue+']').appendTo(filteredSelect) .filter(':first').attr('selected','selected'); }).trigger('change'); return this; } })(jQuery); </code></pre> <p>Which does work in IE, but still has two problems. The <code>.filter(':first').attr('selected','selected');</code> bit doesn't seem to do anything in IE (it should select the first visible element). Since I've been using <code>appendTo</code> it currently defaults to the last. And the other problem is that since I detach all the elements immediately, you can't have default values in your HTML.</p>
    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.
 

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