tags:

views:

192

answers:

4

I can disable one and one by doing this:

jQuery('#ListBoxA').attr('disabled','true');

But how can I apply this to multiple items? This does not work:

jQuery('#ListBoxA, #ListBoxB, #ListBoxC').attr('disabled','true');

UPDATE

Not much markup to show....

<select id="ListBoxA" size="4" name="countrySelectBox">
<select id="ListBoxB" size="4" name="cityListBox">
<select id="ListBoxC" size="4" name="storeListBox" multiple="multiple">
A: 
jQuery('select').each(function(){
    jQuery(this).attr('disabled','true');
});
qwertypants
This will not work, since I do not want to disable all listboxes on the page. Just a selected few.
Steven
If you put the ones you want in a container, then you can further update the selector appropriately using jQuery('#containerDiv select')
qwertypants
+1  A: 

For another option, if you give them all a CSS class, you can do:

jQuery('.ListBoxClass').attr('disabled', true);
Walter Mundt
Just like to add to this (correct) answer that it should technically be `attr('disabled', true)` (or `false`) as opposed to passing it the string `'true'`.
thenduks
Thanks, I've fixed the code.
Walter Mundt
Correct. But I want to do this with ID's :)
Steven
Darin states above that I should use 'disabled' instead of 'true'. Anny comment on that?
Steven
+3  A: 

jQuery('#ListBoxA,#ListBoxB,#ListBoxC').attr('disabled','disabled'); (Without white spaces).

To enable use jQuery('#ListBoxA,#ListBoxB,#ListBoxC').removeAttr('disabled');

http://docs.jquery.com/Selectors/multiple#selector1selector2selectorN

Cesar
Ah, thanks. It was the empty spaces. I got that from an example I found on the web.
Steven
Yeah, just discovered the removeAttr. Before that I used `.attr('disabled','');`
Steven
+1  A: 

You could also use attribute filters if the boxes you want to disable share an attribute (in this case any one with 'ListBox' in it's id:

$("select[id*='ListBox']").attr('disabled',true);
Mark
Hmm... ok. What is the gain of doing attribute filtering?
Steven
@Steven, it could be of use if the selects are generated dynamically and you do not know how many there will be until runtime.
Mark