Enable/Disable a button if check boxes are checked/unchecked

checkbox, jquery

Solution

The overall function can be simplified as

$(function(){
    var checkboxes = $(':checkbox:not(#SelectAll)').click(function(event){
        $('#Assign').prop("disabled", checkboxes.filter(':checked').length == 0);
    });

    $('#SelectAll').click(function(event) {   
        checkboxes.prop('checked', this.checked);
        $('#Assign').prop("disabled", !this.checked)
    });
});

Demo: Fiddle

Problem

I have multiple checkboxes in a table and I have written the code for selecting/deselecting all checkboxes and enabling/disabling a button with it. (Select All Checkbox is the header of the column) ``` $(function(){ $('#SelectAll').click(function(event) { if(this.checked) { // Iterate each checkbox $(':checkbox').each(function() { this.checked = true; }); $('#Assign').removeAttr("disabled"); } if(!this.checked) { // Iterate each checkbox $(':checkbox').each(function() { this.checked = false; }); $('#Assign').attr("disabled","disabled"); } }); }); ``` Although if a user decides to check the checkboxes individually and uncheckes all in he process how do I write the below code to check if all of them are unchecked and disable the button ``` $(function(){ $(':checkbox').click(function(event){ if(this.checked){ $('#Assign').removeAttr("disabled"); } if(!this.checked){ //Need to put code here to check if all of them are unchecked { //disable the Assign button if all are unchecked by user $('#Assign').attr("disabled","disabled"); } } }); }); ```

Original source