I have a header checkbox and subsequent "children" checkboxes that are displayed programmatically each time my app loads.
I'm trying to ensure that if the user unchecks all the children boxes, it auto unchecks the header. I map through all checkboxes on the page at the start:
var x = '';
$(document).ready(function() {
$('.class').map(function () {
x += ('Yes');
});
alert(x);
});
This gives me an array with "Yes" for the number of checkboxes that exist in each section.
I need to be able to slice the array into nothing as the user unchecks boxes within that class. I'm stuck.
Here is my change listener:
$(':checkbox').change(function () {
if(**THE CHECKBOX I AM UNCHECKING HAS THE CLASS**) {
x.slice(-1);
}
});
SOLUTION:
<input class='children' type='checkbox'/>
<input class='children' type='checkbox'/>
<input class='header' type='checkbox'/>
var check_count = $(".children:checked").length;
$(".children").change(function () {
if(!this.checked) {
if (check_count == 0) {
$(".header").prop("checked", false);
}
}
});