views:

8380

answers:

3

Say I have this dropdown:

<select id="theOptions1">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>

I want it so that when the user selects 1, this is the thing that the user can choose for dropdown 2:

<select id="theOptions2">
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
</select>

Or if the user selects 2:

<select id="theOptions2">
  <option value="a">a</option>
  <option value="b">b</option>
</select>

Or if the user selects 3:

<select id="theOptions2">
  <option value="b">b</option>
  <option value="c">c</option>
</select>

I tried the code posted here: http://stackoverflow.com/questions/877328/jquery-disable-select-options-based-on-radio-selected-need-support-for-all-brows

But it doesn't work for selects.

Please help! Thank you!

UPDATE: I really like the answer Paolo Bergantino had on: http://stackoverflow.com/questions/877328/jquery-disable-select-options-based-on-radio-selected-need-support-for-all-brows

Is there anyway to modify this to work with selects instead of radio buttons?

jQuery.fn.filterOn = function(radio, values) {
    return this.each(function() {
        var select = this;
        var options = [];
        $(select).find('option').each(function() {
            options.push({value: $(this).val(), text: $(this).text()});
        });
        $(select).data('options', options);
        $(radio).click(function() {
            var options = $(select).empty().data('options');
            var haystack = values[$(this).attr('id')];
            $.each(options, function(i) {
                var option = options[i];
                if($.inArray(option.value, haystack) !== -1) {
                    $(select).append(
                    $('<option>').text(option.text).val(option.value)
                    );
                }
            });
        });            
    });
};
+1  A: 

For the record you can NOT disable options in a select list in Internet Explorer.

scunliffe
I think he means remove rather than disable.
karim79
Ya sorry, I meant remove...
Sea4Me
Oh dear Flying Spaghetti Monster! It's a URL shortener!
random
+3  A: 

This works (tested in Safari 4.0.1, FF 3.0.13):

$(document).ready(function() {
    //copy the second select, so we can easily reset it
    var selectClone = $('#theOptions2').clone();
    $('#theOptions1').change(function() {
        var val = parseInt($(this).val());
        //reset the second select on each change
        $('#theOptions2').html(selectClone.html())
        switch(val) {
            //if 2 is selected remove C
            case 2 : $('#theOptions2').find('option:contains(c)').remove();break;
            //if 3 is selected remove A
            case 3 : $('#theOptions2').find('option:contains(a)').remove();break;
        }
    });
});

And the beautiful UI:

<select id="theOptions1">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
</select>
<br />
<select id="theOptions2">
  <option value="a">a</option>
  <option value="b">b</option>
  <option value="c">c</option>
</select>
karim79
I will try that right now. I like how you keep the UI pretty
Sea4Me
This example of your saved me a days work. Thanks!
Yogi Yang 007
+4  A: 

You can add classes to your <option>s to store which go with each value of #theOptions1:

<select id="theOptions2">
  <option value="a" class="option-1 option-2">a</option>
  <option value="b" class="option-1 option-2 option-3">b</option>
  <option value="c" class="option-1 option-3">c</option>
</select>

then do this:

$(function() {
    var allOptions = $('#theOptions2 option').clone();
    $('#theOptions1').change(function() {
        var val = $(this).val();
        $('#theOptions2').html(allOptions.filter('.option-' + val));
    });
});
kevingessner
I like this better.Just a small correction - it should be:$('#theOptions2').html(...
Daniel Alexiuc
Pretty slick. Modifies the presentational code, but all in all, very extend-able.
Tchalvak