-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.js
30 lines (25 loc) · 814 Bytes
/
logic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var parent = document.getElementById("parent");
var children = document.getElementsByName('child');
//If the parent is checked, set all children to checked and vice versa
parent.onchange = allCheck;
for (var i = 0; i < children.length; i++) {
children[i].onchange = childChanged;
}
//If the parent is checked, set all children to checked and vice versa
function allCheck() {
for (var i = 0; i < children.length; i++) {
children[i].checked = parent.checked;
}
}
//If any child is unchecked, uncheck the parent.
function childChanged() {
if (!this.checked) {
parent.checked = false;
return;
}
//If all children are checked, check the parent.
for (var i = 0; i < children.length; i++) {
if (!children[i].checked) return;
}
parent.checked = true;
}