Skip to content

Instantly share code, notes, and snippets.

@fyulistian
Created July 26, 2018 06:04
Show Gist options
  • Save fyulistian/c0226f5623cbf1f4e7d60ae3f0fddf43 to your computer and use it in GitHub Desktop.
Save fyulistian/c0226f5623cbf1f4e7d60ae3f0fddf43 to your computer and use it in GitHub Desktop.
jQuery checkbox check all, but if one not checked, primary checkbox become uncheck
// document ready
$(document).ready(function() {
// event on change
$(document).on("change","#box-all",function() {
// check all
if(this.checked) {
$(".box-single").each(function() {
this.checked = true;
})
// un-check all
} else {
$(".box-single").each(function() {
this.checked = false;
})
}
});
// event on click
$(document).on("click",".box-single",function() {
// if all check box single are checked
if ($(this).is(":checked")) {
var isAllChecked = 0;
$(".box-single").each(function() {
if(!this.checked) isAllChecked = 1;
})
// box all get check
if(isAllChecked == 0) $("#box-all").prop("checked", true);
} else {
// box all get un-check
$("#box-all").prop("checked", false);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment