Steven
Steven

Reputation: 19425

How do I disable multiple listboxes in one go using jQuery?

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

Upvotes: 6

Views: 6665

Answers (5)

Adil Aslam Sachwani
Adil Aslam Sachwani

Reputation: 964

The problem was with the whitespace

For disabling items:

$("#ListBoxA,#ListBoxB,#ListBoxC").prop("disabled", true);

For enabling items:

$("#ListBoxA,#ListBoxB,#ListBoxC").prop("disabled", false);

Upvotes: 1

Cesar
Cesar

Reputation: 3519

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

Upvotes: 11

Mark
Mark

Reputation: 108537

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);

Upvotes: 1

Walter Mundt
Walter Mundt

Reputation: 25271

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

jQuery('.ListBoxClass').attr('disabled', true);

Upvotes: 1

Wilkins
Wilkins

Reputation: 800

jQuery('select').each(function(){
    jQuery(this).attr('disabled','true');
});

Upvotes: 0

Related Questions