Created
July 26, 2018 06:04
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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