GEOFFREY MWANGI GEOFFREY MWANGI - 1 month ago 7
jQuery Question

Disable a button in jquery if checkboxes with a certain class value are checked

I have checkboxes generated dynamically through php and i would like to hide a certain button if all the checkboxes having a similar class value are not checked and reenable it if they are checked:

The php code that generates the checkboxes is

<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Description</th>
<th>Status</th>
<th>Comment</th>
</tr>
</thead>
<tbody>

foreach ($checks as $m => $check) {
$item ="";
$checkbox ="";
$textinput ="";
$displayx="";

if ($check->mandatory_customer == 1) { //mandatory customer checks
$displayx .="<i style='color:red;'>*</i>";
$item .= $check->item.$displayx;
$checkbox .='<input type="checkbox" class="1" id="'.$m.'"' ;
$textinput .='<input type="text" class="1" id="'.$m.'"' ;

} else { //not mandatory customer
$item .= $check->item;
$checkbox .='<input type="checkbox" class="0" id="'.$m.'"' ;
$textinput .='<input type="text" class="0" id="'.$m.'"' ;

}

echo "<tr id='" . $m . "'>";
echo "<td>" . $m . "</td>";
echo "<td>" . $item . "</td>";
echo "<td>".$checkbox."</td>";
echo "<td>".$textinput."</td>";
echo "</tr>";
}
?>
}

</tbody>





I also have a button that i would like to disable if all checkboxes with class 1 are not checked and enable it if they are all checked using jquery

<button class="btn btn-success" id="approve_btn">Approve</button>


I have tried:

$('tbody tr').each(function () {
if ($(this).attr('class:1').find('.myCheckBox').prop('checked')) {
doEnableButton = true;
}
if (!doEnableButton) {
$('#approve_btn').prop('disabled', 'disabled')
}
else {
$('#approve_btn').removeAttr("disabled");
}
});


});

I have checked on several resources but most of them are not helpful
The above fails how do i go about this

Answer

Try this

    $(document).ready(function () {
        $(":checkbox.1").on("change", function () {
            if ($(":checkbox.1").not(":checked").length > 0)
                $("#approve_btn").prop("disabled", "disabled");
            else
                $("#approve_btn").removeAttr("disabled");
        });
    });
Comments