views:

8423

answers:

4
+3  Q: 

jQuery Checkboxes

Hi All,

I'm trying to write a piece of jQuery code where, if all checkboxes are "unchecked", then all li tags have the class "disabled."

But, if one checkbox (any checkbox) is checked, then all [li] tags lose the class "disabled".

Many thanks!

+4  A: 
$(':checkbox').click(function () {
    $('li').toggleClass('disabled', !$(':checkbox:checked').length);
});
RaYell
You should use $('#myform input[type=checkbox]:checked') if you only want the checkboxes in a form with id myform.
Time Machine
I think you meant $("li") instead of $("*").
Philippe Leybaert
Yes, I notices that typo and fixed it.
RaYell
You can just use `if ($('input[type=checkbox]:checked').length)` rather than using the extra `count` variable.
Matt Sach
I believe that you will also want to change your else if (count === 1) to else if (count >= 1). Otherwise, if more than one box is checked this will fail. Otherwise, very nice.
Sean Vieira
Read the problem description. It says if one checkbox is selected then it should remove the class not if at least one. Also I'm using the `count` variable to make the script faster.
RaYell
Aberon, user hasn't clarified whether he wants to remove the class if at least one checkbox is checked or EXACTLY one checkbox is checked.
SolutionYogi
You should use the click event instead of the change event.
Philippe Leybaert
You can replace the branch with a call to toggleClass instead of two different calls (one to addClass and one to removeClass): $('li').toggleClass('disabled', (count===0));
Ken Browning
There is a shortcut for getting checkboxes instead of checking the type attribute:$(':checkbox:checked')
MacAnthony
@Philippe Leybaert> it doesn't really matter if I use change or click event in this case
RaYell
Ken Browning>I could replace that if we would remove class if at least one is selected. If we have a case either exactly 1 or none is selected then I'm afraid it cannot be shorten.
RaYell
@MacAnthony> thanks for pointing that out, updated my code
RaYell
Ken, thanks for pointing out the toggleClass method, very cool. :)
SolutionYogi
I've just updated the code. It now covers both cases (remove class if exactly one checkbox is checked and remove it if at least one is checked)
RaYell
change or click does make a big difference. Have you tried it in IE and Firefox?
Philippe Leybaert
Yes, 'change' is broken in IE, 'click' would be preferable. Also, you should edit/remove the first code snipped as it removes the class only when exactly one checkbox is clicked and user clarified that he wants to remove the class if at least one checkbox is clicked.
SolutionYogi
Updated, thanks guys for tips.
RaYell
A: 
$(':checkbox')
    .click(
        function() 
        { 
            $('li').toggleClass('disabled', $(':checkbox :checked').length <= 0));
        }
     );

EDIT: Thanks Ken for pointing out toggleClass method.

SolutionYogi
+1  A: 

Slight modification of Phillipe Leybaert's, which will include any dynamically added checkboxes:

$('input[type=checkbox]').live('click', function () {
    if ($('input[type=checkbox]:checked').length)
    {
        $('li').addClass('disabled');
    }
    else
    {
        $('li').removeClass('disabled');
    }
});
Matt Sach
"change" is not a good event to capture for checkboxes. You should always use "click" (which is also fired when a checkbox is selected using the keyboard).
Philippe Leybaert
Excellent point, and one I should have remembered from previous code. IE doesn't handle "change" on checkboxes, for starters.
Matt Sach
A: 

$(':checkbox') .click( function() { $('li').toggleClass('disabled', $(':checkbox :checked').length <= 0)); } );

Claudio