Question

I have three drop down select lists, when a value from one of the list is selected, I want the other two to be greyed out and inactive. I have id wrappers around each select list that I'm experimenting on, but as I am new with jquery I'm not getting very far.

Was it helpful?

Solution

Something like this should work:

$(function() {
  var $selects = $('#select1, #select2, #select3');
  $selects.change(function() {
    // disabled the other two
    $selects.not(this).attr('disabled', 'disabled');
  });
});


Updated version:

$(function() {
  var $selects = $('#select1, #select2, #select3');
  $selects.change(function() {
    // disable or enable the other two
    $selects.not(this).attr('disabled', $(this).val() === '' ? '' : 'disabled');
  });
});

OTHER TIPS

I assume you have three, and if any of the three are selected, the other two should disable.

function toggleOthers(x) {
var select = new Array('#wrapper1','#wrapper2','#wrapper3');

for (int i = 0; i < select.length; i++ )
    if ($(x).attr('id')!=select[i])
        $(x).attr('disabled','disable');
}

HTML: <select onchange='toggleOthers(this);' id='wrapper1'> etc...

You can do something like this:

$('#wrappers').change(function(){
  $('#select1').attr('disabled', true);
  $('#select2').attr('disabled', true);
});

Where wrappers is the id of select element from which when you choose a value the other two get disabled whose id is select1 and select2 respectively eg:

<select id="wrappers">.........
<select id="select1">.........
<select id="select2">.........

Here is working example

if you have the selects with a div wrapper with an id of #wrapper

$('#wrapper select').each(function(i,select){
   $(select).change(function(){
        $(this).attr('class','enabled').siblings().attr('class','disabled');
    })
})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top