I am using iCheck plugin for checkbox. I put a "Select all" functionality at there. The structure:
It works fine like when check on "Select all", all the checkboxes will checked. when uncheck "Select all", all the checkboxes will unchecked. But, after checking "Select all", if making any checkbox unchecked, "Select all" should be unchecked too automatically as at that time all checkbox are not checked.
To make this, I have written this:
$('#check-all').on('ifChanged', function(event){
if($('.check').filter(':checked').length == $('.check').length) {
$('#check-all').iCheck('check');
} else {
$('#check-all').iCheck('uncheck');
}
$('#check-all').iCheck('update');
});
But, after putting this code, my checkboxes are not working fine like "Select all" doesn't work with single click, it need multiple click often. Also "Select all" is not unchecking when any single checkbox is unchecked. What's the problem with the code? How to write it correctly?
Fiddle work
In order to select all the checkboxes of a page, we need to create a selectAll () function through which we can select all the checkboxes together. In this section, not only we will learn to select all checkboxes, but we will also create another function that will deselect all the checked checkboxes.
Clicking on the master checkbox selects all checkboxes; and unchecking it, deselects all checkboxes.
// Remove the checked state from "All" if any checkbox is unchecked
$('.check').on('ifUnchecked', function (event) {
$('#check-all').iCheck('uncheck');
});
// Make "All" checked if all checkboxes are checked
$('.check').on('ifChecked', function (event) {
if ($('.check').filter(':checked').length == $('.check').length) {
$('#check-all').iCheck('check');
}
});
handling $('#check-all').on('ifUnchecked', ...
is tricky though - it fires the other handling and every checkbox gets unchecked
fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With