-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckur.js
43 lines (35 loc) · 1.33 KB
/
checkur.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
var Checkur = (function() {
function findCheckboxesByClassName(className) {
const checkboxes = document.querySelectorAll(`input[type="checkbox"].${className}`);
return Array.from(checkboxes);
}
function addCheckboxEventHandlers(className) {
const checkboxes = findCheckboxesByClassName(className);
checkboxes.forEach((checkbox) => {
checkbox.addEventListener('change', function() {
if (this.checked) {
console.log("This checkbox is checked.");
} else {
console.log("This checkbox is unchecked.");
}
localStorage.setItem(`${className}_${checkbox.id}`, this.checked);
});
});
}
function restoreCheckboxStateFromLocalStorage(className) {
const checkboxes = findCheckboxesByClassName(className);
checkboxes.forEach((checkbox) => {
const storedState = localStorage.getItem(`${className}_${checkbox.id}`);
if (storedState !== null) {
checkbox.checked = storedState === 'true';
}
});
}
function init(className) {
restoreCheckboxStateFromLocalStorage(className)
addCheckboxEventHandlers(className)
}
return {
init: init
};
})();