-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximum_checker.js
48 lines (48 loc) · 1.55 KB
/
maximum_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
/* global document,alert */
class MaximumChecker {
constructor(selector) {
this.form = document.querySelector(selector.formSelector);
this.allDays = document.querySelectorAll(selector.allDaysSelector);
this.noneCheckbox = this.form.querySelector(selector.noneCheckboxSelector);
this.selectedThree = [];
this.totalDays = this.allDays.length;
}
selectedDays() {
for (let i = 0; i < this.totalDays; i++) {
this.allDays[i].addEventListener("click", () => {
if (this.allDays[i].checked === true) {
this.noneCheckbox.checked = false;
this.countCheck(i);
}
else {
this.selectedThree.splice(i, 1);
}
});
}
this.noneCheckbox.addEventListener("click", this.noneChecked.bind(this));
}
countCheck(index) {
if (this.selectedThree.length < 3) {
this.selectedThree.push(index);
}
else {
this.allDays[index].checked = false;
alert("Only 3 days can be selected. You have already selected " + this.allDays[this.selectedThree[0]].value + "," + this.allDays[this.selectedThree[1]].value + " and " + this.allDays[this.selectedThree[2]].value + ".");
}
}
noneChecked() {
for (var i = 0; i < this.allDays.length; i++) {
this.allDays[i].checked = false;
}
this.selectedThree = [];
}
}
window.onload = () => {
var selector = {
formSelector: "[data-form=true]",
allDaysSelector: "[data-checkbox=true]",
noneCheckboxSelector: "[data-nonecheckbox=true]"
};
var obj = new MaximumChecker(selector);
obj.selectedDays();
};