Monday, February 21, 2011: jQuery: Checkboxes Toggling Plugin « from the old blog archive »

In hurry, I made this simple plugin to jQuery:

    $.fn.toggleCheckboxes = function() {
        var state = false;
        $('input:checkbox', this).each(function() {
            if (!this.checked) {
                state = true;
                return false;
            }
        }).each(function() {
            this.checked = state;
        })
    };

It toggles all the checkboxes inside the matched elements all at once.

This allows you to do something like

$('.toggle').click(function() {
    $(this).closest('tr').toggleCheckboxes();
});