I want to implement a "Select all" using iCheck.
This is what I've done so far:
$(function () {
$('input').iCheck();
$('input.all').on('ifChecked ifUnchecked', function(event) {
if (event.type == 'ifChecked') {
$('input.check').iCheck('check');
} else {
$('input.check').iCheck('uncheck');
}
});
$('input.check').on('ifUnchecked', function(event) {
$('input.all').iCheck('uncheck');
});
});
Fiddle: jsfiddle.net/N7uYR/1/
I want that when "Check Box 1", "Check Box 2", "Check Box 3", "Check Box 4" are selected, "Select all" also gets selected.
Exactly like this: jsfiddle.net/ivanionut/N7uYR/
How can I do this?
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.
The Best Answer is const value = $('SELECTOR'). iCheck('update')[0]. checked; This directly returns true or false as boolean .
type == 'checkbox' && inputs[x]. name == 'us') { is_checked = inputs[x]. checked; if(is_checked) break; } } // is_checked will be boolean 'true' if any are checked at this point.
To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);
Here's what I came up with.
$(function () {
var checkAll = $('input.all');
var checkboxes = $('input.check');
$('input').iCheck();
checkAll.on('ifChecked ifUnchecked', function(event) {
if (event.type == 'ifChecked') {
checkboxes.iCheck('check');
} else {
checkboxes.iCheck('uncheck');
}
});
checkboxes.on('ifChanged', function(event){
if(checkboxes.filter(':checked').length == checkboxes.length) {
checkAll.prop('checked', 'checked');
} else {
checkAll.removeProp('checked');
}
checkAll.iCheck('update');
});
});
jsFiddle
For anyone still struggling with this, I've altered the accepted answer https://stackoverflow.com/a/17821003/3061836) from Dzulqarnain Nasir a bit.
I've changed the removeProp
method to the prop
method:
checkAll.removeProp('checked');
Is altered to
checkAll.prop('checked', false);
The removeProp
method didn't work for me, but setting the checked property to false worked.
At the time of writing, I'm using iCheck 1.0.2 with jQuery 2.1.4
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