-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
54 lines (43 loc) · 1.37 KB
/
script.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
const checkboxes = document.querySelectorAll('input[type="checkbox"]');
let lastChecked;
function handleCheck(e) {
// Check if the shift key was down
// And check that they are checking it
let inBetween = false;
if (e.shiftKey && this.checked) {
// Loop over every checkbox
checkboxes.forEach((checkbox) => {
// Check if this is the first or last checkbox that was checked
if (checkbox === this || checkbox === lastChecked) {
inBetween = !inBetween;
}
// If the checkboxes are in between the two checked boxes, check them
if (inBetween) {
checkbox.checked = true;
}
});
}
// Set the last checked box
lastChecked = this;
}
checkboxes.forEach((checkbox) =>
checkbox.addEventListener("click", handleCheck)
);
// or
// const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]');
// let lastChecked;
// function handleCheck(e) {
// let inBetween = false;
// if(e.shiftKey && this.checked){
// checkboxes.forEach(checkbox => {
// if(checkbox === this || checkbox === lastChecked){
// inBetween = !inBetween;
// }
// if (inBetween){
// checkbox.checked = true;
// }
// })
// }
// lastChecked = this;
// }
// checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));