JavaScript How to get Indeterminate Checkbox to fire function of other checkboxes?

I made a table that has checkboxes that show and hide rows to make it easier to compare specific rows. I wanted to add indeterminate checkboxes so that multiple rows can be selected or not selected at the same time. It kinda works in that it checks the boxes under it but doesn't fire the function to hide or show the row even though its checked. The individual check boxes still fire the function though.

jsfiddle

Show/hide rows with checkbox

$('input[type = checkbox]').change(function () {
var valu = $(this).val();
var ischecked = $(this).is(":checked");

if( ischecked ){
    $('.' + valu).show();
}else{
    $('.' + valu).hide();
}
});

Indeterminate Checkbox for division 1

$(document).ready(function () {
var checkboxes = document.querySelectorAll('input.div1'),
checkdiv1 = document.getElementById('checkdiv1');

for(var i=0; i<checkboxes.length; i++) {
checkboxes[i].onclick = function() {
    var checkedCount = document.querySelectorAll('input.div1:checked').length;

    checkdiv1.checked = checkedCount > 0;
    checkdiv1.indeterminate = checkedCount > 0 && checkedCount < checkboxes.length;
}
}

checkdiv1.onclick = function() {
for(var i=0; i<checkboxes.length; i++) {
    checkboxes[i].checked = this.checked;
}
}
});

Indeterminate Checkbox for division 2

$(document).ready(function () {
var checkboxes = document.querySelectorAll('input.div2'),
checkdiv2 = document.getElementById('checkdiv2');

for(var i=0; i<checkboxes.length; i++) {
checkboxes[i].onclick = function() {
    var checkedCount = document.querySelectorAll('input.div2:checked').length;

    checkdiv2.checked = checkedCount > 0;
    checkdiv2.indeterminate = checkedCount > 0 && checkedCount < checkboxes.length;
}
}

checkdiv2.onclick = function() {
for(var i=0; i<checkboxes.length; i++) {
    checkboxes[i].checked = this.checked;
}
}
});
Answer:1

1) You need to add hide/show rows based on inner check boxes, so you can bind 'change' event handler to check boxes with class "show". So, you code would be

$('input[type = checkbox].show').change(function () {

instead of

$('input[type = checkbox]').change(function () {

2) You can remove second $(document).ready() function and combine your code into one.

$(document).ready(function(){ fun1() });
$(document).ready(function(){ fun2() });

is equivalent to

$(document).ready(function(){ fun1(); fun2(); });

3) You can trigger 'change' event of inner check boxes when outer check box is clicked:

$(checkboxes[i]).trigger('change');

$('input[type = checkbox].show').change(function () {
    
    var valu = $(this).val();
    var ischecked = $(this).is(":checked");
    
    if( ischecked ){
        $('.' + valu).show();
    }else{
        $('.' + valu).hide();
    }
});

$(document).ready(function () {
  	var checkboxes = document.querySelectorAll('input.div1');
    var checkdiv1 = document.getElementById('checkdiv1');

    for(var i=0; i<checkboxes.length; i++) {
        checkboxes[i].onclick = function() {
            var checkedCount = document.querySelectorAll('input.div1:checked').length;

            checkdiv1.checked = checkedCount > 0;
            checkdiv1.indeterminate = checkedCount > 0 && checkedCount < checkboxes.length;
        }
    }

    checkdiv1.onclick = function() {
        var checkboxes = document.querySelectorAll('input.div1');
        for(var i=0; i<checkboxes.length; i++) {
            checkboxes[i].checked = this.checked;        
            $(checkboxes[i]).trigger('change');
        }
    }

  	var checkboxes = document.querySelectorAll('input.div2');
    var checkdiv2 = document.getElementById('checkdiv2');

    for(var i=0; i<checkboxes.length; i++) {
        checkboxes[i].onclick = function() {
            var checkedCount = document.querySelectorAll('input.div2:checked').length;

            checkdiv2.checked = checkedCount > 0;
            checkdiv2.indeterminate = checkedCount > 0 && checkedCount < checkboxes.length;
        }
    }

    checkdiv2.onclick = function() {
        var checkboxes = document.querySelectorAll('input.div2');
        for(var i=0; i<checkboxes.length; i++) {
            checkboxes[i].checked = this.checked;
            $(checkboxes[i]).trigger('change');
        }
    }
    
    $('.hideRow').click(function () {
        $('input[value="' + $(this).attr('checkBoxValue') + '"]').trigger('click');
    });
});
.hide{
    visibility: hidden;
}

label {
    display:block
}

label:first-child {
    font-weight:bold;
}

label:nth-child(n+2) {
    margin-left:1em;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div>
	<label><input type="checkbox" id="checkdiv1" checked> Division 1</label>
<label><input type="checkbox" class="div1 show" value="d1" checked> Team 1</label>
<label><input type="checkbox" class="div1 show" value="d2" checked > Team 2</label>
<label><input type="checkbox" class="div1 show" value="d3" checked> Team 3</label></div>
<div>
<label><input type="checkbox" id="checkdiv2" checked> Division 2</label>
<label><input type="checkbox" class="div2 show" value="r1" checked> Team 1</label>
<label><input type="checkbox" class="div2 show" value="r2" checked > Team 2</label>
<label><input type="checkbox" class="div2 show" value="r3" checked> Team 3</label></div>
<br>
<hr>
    
<br>
<table border="1">
    <tbody>
    	<tr>            
            <td></td>
            <td >Teams</td>
            <td>Points For</td>
            <td>Points Against</td>
        </tr>
        <tr class="d1">
            <td><button class="hideRow" checkBoxValue="d1">Hide</button></td>
            <td>Division 1 Team 1</td>
            <td>280</td>
            <td>100</td>
        </tr>
        <tr class="d2">
            <td><button class="hideRow" checkBoxValue="d2">Hide</button></td>
            <td>Division 1 Team 2</td>
            <td>225</td>
            <td>150</td>
        </tr>
        <tr class="d3">
            <td><button class="hideRow" checkBoxValue="d3">Hide</button></td>
            <td>Division 1 Team 3</td>
            <td>187</td>
            <td>223</td>
        </tr>
        <tr class="r1">
            <td><button class="hideRow" checkBoxValue="r1">Hide</button></td>
            <td>Division 2 Team 1</td>
            <td>330</td>
            <td>185</td>
        </tr>
        <tr class="r2">
            <td><button class="hideRow" checkBoxValue="r2">Hide</button></td>
            <td>Division 2 Team 2</td>
            <td>267</td>
            <td>225</td>
        </tr>
        <tr class="r3">
            <td><button class="hideRow" checkBoxValue="r3">Hide</button></td>
            <td>Division 2 Team 3</td>
            <td>223</td>
            <td>150</td>
        </tr>
    </tbody>
</table>
Answer:2

I'm trying to crawl a webpage using Nightmare, but want to wait for #someelem to be present, only if it actually exists. Otherwise, I want Nightmare to move on. How can this be done using .wait()? I ...

I'm trying to crawl a webpage using Nightmare, but want to wait for #someelem to be present, only if it actually exists. Otherwise, I want Nightmare to move on. How can this be done using .wait()? I ...

I'm trying to figure out how to trigger a link button to trigger and change all checkboxes to checked when clicked upon. When I click the button, it will bring up the alert message, but not triggering ...

I'm trying to figure out how to trigger a link button to trigger and change all checkboxes to checked when clicked upon. When I click the button, it will bring up the alert message, but not triggering ...

  1. trigger check checkbox jquery
  2. trigger checkbox check
  3. trigger checkbox check event

I am currently working on an problem with a login page for an AngularJS app. The login page uses the $http service to submit the username and password using Basic authentication (Authorization: Basic ...

I am currently working on an problem with a login page for an AngularJS app. The login page uses the $http service to submit the username and password using Basic authentication (Authorization: Basic ...

I have this code : var dynamicVarName = id; var percentage = res.parkeren[0].dynamicVarName.percentage; DynamicvarName can have different values, but when i call this in the var percentage it's ...

I have this code : var dynamicVarName = id; var percentage = res.parkeren[0].dynamicVarName.percentage; DynamicvarName can have different values, but when i call this in the var percentage it's ...