views:

48

answers:

4

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.

A: 

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

Sarfraz
A: 

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...

Robert
+3  A: 

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');
  });
});
jmar777
This is very elegant and easy to understand. Each of the dropdown lists has a "None selected" option. The value there is just "". If you want the other fields to go active again if "None selected" is selected, how would you write it?
Toxid
Added an updated version that should enable or disable the other two depending on the current value of the select that triggered the change event. Thanks for the accept!
jmar777
Simply fantastic! You had one too many = though, should be .val() ==. I take it that's a typo.
Toxid
That was actually intentional. The triple === means "don't do any type coercion in the comparison", which is pretty important when comparing to string values that may be empty.
jmar777
Oh, good to know. Yesterday when I tried with triple, it didn't work. Today it does, I guess I had some kind of browser cache issue.
Toxid
Strange... not sure why that initially didn't work for you. I was wrapping things up when I commented last night so I commented quickly, but if you were interested in more reading on == vs. ===, here's a previous SO post that sums it up pretty well: http://stackoverflow.com/questions/359494/javascript-vs
jmar777
A: 

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');
    })
})
pferdefleisch