SMTDev
SMTDev

Reputation:

jQuery Checkboxes

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!

Upvotes: 11

Views: 50157

Answers (4)

Garytxo
Garytxo

Reputation: 346

I came across this post by accident and I thought I would add my shilling worth:

jQuery(':checkbox').click(function()
{
    if (jQuery(this).is(':checked'))
    {
        alert("Checked");
    }
    else
    {
        alert("Unchecked");
    }
});

Upvotes: 8

Matt Sach
Matt Sach

Reputation: 1170

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

$(':checkbox').live('click', function () {
    $('li').toggleClass('disabled', !$(':checkbox:checked').length);
});

Upvotes: 5

RaYell
RaYell

Reputation: 70404

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

Upvotes: 15

SolutionYogi
SolutionYogi

Reputation: 32223

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

EDIT: Thanks Ken for pointing out toggleClass method.

Upvotes: 1

Related Questions