Tharu perera Tharu perera - 1 month ago 9
jQuery Question

How to disable a button if no checked boxes are clicked

I have checkbox column in my data table and I want to hide a button if none of checked boxes clicked.[including pagination].
below is my code I used to get the values when clicking button,pls advice,

$('#button').click(function () {
var id = "";
var oTable = $("#userTable").dataTable();
$(".groupCheckBox:checked", oTable.fnGetNodes()).each(function () {
if (id != "") {
id = id + "," + $(this).val();
} else {
id = $(this).val();
}

});
});

Answer

You can add an event listener to all of your checkboxes. Whenever something changes, you check if all of them are un-checked => disable the button, else => enable the button.

$(".groupCheckBox").on('change', function(){
    if($(".groupCheckBox:checked", oTable.fnGetNodes()).length == 0){
        $('#merge_button').prop('disabled', true);
    }else{
        $('#merge_button').prop('disabled', false);
    }
});