How to clear all checkbox When checkbox id=\"checkAll\"
are checked ?
in my demo , when user checked checkbox id=\"checkAll\"
all checkbox are
If I'm understanding you correctly, you want to clear all the items when they click "Check All" - you might want to change the name to "Clear All" for clarity.
If so, just add a simple ! in front of this.checked
$('#checkAll').click(function () {
$('input:checkbox').prop('checked', !this.checked); });
You shouldn't use the same ID for multiple elements, since IDs are unique. Instead use class="checkedItem"
.
If you want to clear all use
$('#checkAll').click(function () {
$('input:checkbox').prop('checked', !this.checked);
});
However this also clears the first checkbox. To prevent this use
$('#checkAll').click(function () {
$('input:checkbox').not(this).prop('checked', !this.checked);
});
You can use change()
function and it you can check by using this.checked
inside the change event, it will return boolean
value for checkbox checked status :
$('#checkAll').change(function () {
$('input:checkbox').prop('checked', this.checked); // all checked on check of #checkAll
});
$('#checkAll').change(function () {
$('input:checkbox').prop('checked', !this.checked); // all unchecked on check of #checkAll
});
if you want all unchecked but no the main check box then:
$('#checkAll').change(function () {
$('input:checkbox').not(this).prop('checked', !this.checked); // all unchecked on check of #checkAll but not itself
});
Just apply the negation of this.checked
$('#checkAll').change(function () {
$('input:checkbox').not(this).prop('checked', !this.checked);
});
Demo