-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparent_child_checker.js
67 lines (62 loc) · 1.98 KB
/
parent_child_checker.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* global document,window */
class ParentChckbox {
constructor(parentCheckboxes, childCheckboxes, divChildElements) {
this.parentCheckboxes = parentCheckboxes;
this.childCheckboxes = childCheckboxes;
this.divChildElements = divChildElements;
}
bindEvents() {
this.parentCheckboxes.addEventListener("click", () => {
this.checkEvents();
});
for (let m = 0; m < this.childCheckboxes.length; m++) {
this.childCheckboxes[m].addEventListener("click", () => {
this.unCheckEvents();
});
}
}
checkEvents() {
if (this.parentCheckboxes.checked === true) {
this.divChildElements.style.display = "block";
this.divChildElements.scrollIntoView();
this.checkAllChild();
}
else {
this.divChildElements.style.display = "none";
this.unCheckAllChild();
}
}
unCheckEvents() {
var count = 0;
for (var z = 0; z < this.childCheckboxes.length; z++) {
if (this.childCheckboxes[z].checked === false) {
count++;
}
if (count === this.childCheckboxes.length) {
this.parentCheckboxes.checked = false;
this.divChildElements.style.display = "none";
}
}
}
checkAllChild() {
for (var j = 0; j < this.childCheckboxes.length; j++) {
this.childCheckboxes[j].checked = true;
}
}
unCheckAllChild() {
for (var k = 0; k < this.childCheckboxes.length; k++) {
this.childCheckboxes[k].checked = false;
}
}
}
window.onload = () => {
var childCheckboxes = [];
var divChildElements = [];
var parentCheckboxes = document.querySelectorAll("[ data-checkbox=parent]");
for (var i = 0; i < parentCheckboxes.length; i++) {
childCheckboxes = document.querySelectorAll("[ data-childcheckbox=" + parentCheckboxes[i].name + "]");
divChildElements = document.querySelector("[div-child=" + parentCheckboxes[i].name + "]");
var parentObj = new ParentChckbox(parentCheckboxes[i], childCheckboxes, divChildElements);
parentObj.bindEvents();
}
};