Solved

How to disable a list box based on a checkbox

Posted on 2013-11-07
4
721 Views
Last Modified: 2013-11-07
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
Comment
Question by:mousemat24
  • 2
4 Comments
 
LVL 43

Accepted Solution

by:
Chris Stanyon earned 500 total points
ID: 39629863
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
 

Author Comment

by:mousemat24
ID: 39629869
thanks for that  ChrisStanyon, thats what I wanted!
0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39629877
like this:
$('#chkAllAccounts').on('change', function(){
        $('#lstClients').attr('disabled', $(this).prop('checked'));
});

Open in new window

0
 
LVL 30

Expert Comment

by:Alexandre Simões
ID: 39629880
Or even better:
$('#chkAllAccounts').on('change', function(){
    $('#lstClients').prop('disabled', $(this).prop('checked'));
});

Open in new window

0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question