Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 789
  • Last Modified:

How to disable a list box based on a checkbox

Hi there

Hope someone can help me out, I need some JQuery code.

If I check a check-box called "chkAllAccounts" it will disable a drop down list box called "lstClients"

If I un-check the same check-box called "chkAllAccounts" it will enable the drop down list box called "lstClients"

Hope this makes sense?

thanks
0
mousemat24
Asked:
mousemat24
  • 2
1 Solution
 
Chris StanyonCommented:
Something like this will do it:

$('input[name=chkAllAccounts]').change(function() {
    if ($(this).is(':checked')) {
        $('select[name=lstClients]').prop('disabled', 'disabled');
    } else {
        $('select[name=lstClients]').prop('disabled', false);
    }
});

Open in new window

Here's a fiddle to play with:

http://jsfiddle.net/ChrisStanyon/Z4vRG/
0
 
mousemat24Author Commented:
thanks for that  ChrisStanyon, thats what I wanted!
0
 
Alexandre SimõesManager / Technology SpecialistCommented:
like this:
$('#chkAllAccounts').on('change', function(){
        $('#lstClients').attr('disabled', $(this).prop('checked'));
});

Open in new window

0
 
Alexandre SimõesManager / Technology SpecialistCommented:
Or even better:
$('#chkAllAccounts').on('change', function(){
    $('#lstClients').prop('disabled', $(this).prop('checked'));
});

Open in new window

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now