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.
views:
48answers:
4
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">.........
Sarfraz
2010-08-10 22:42:36
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
2010-08-10 22:48:53
+3
A:
Updated version:
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
2010-08-10 22:50:57
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
2010-08-10 23:10:11
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
2010-08-10 23:22:03
Simply fantastic! You had one too many = though, should be .val() ==. I take it that's a typo.
Toxid
2010-08-10 23:25:50
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
2010-08-11 00:03:34
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
2010-08-11 07:32:56
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
2010-08-11 11:37:06
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
2010-08-10 23:04:29