forked from wesbos/JavaScript30
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
44 lines (38 loc) · 955 Bytes
/
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
const checkboxes = Array.from(document.querySelectorAll("input[type='checkbox']"))
const getFirstChecked = () => {
for (let i = 0; i < checkboxes.length; i++) {
if (checkboxes[i].checked) {
return i
}
}
return -1
}
const getLastChecked = () => {
for (let i = checkboxes.length - 1; i > 0; i--) {
if (checkboxes[i].checked) {
return i
}
}
return -1
}
/**
* Check all in-between boxes
*/
const check = (s, e) => {
for (let i = s; i <= e; i++) {
checkboxes[i].checked = true
}
}
let shiftSelect = function (e) {
if (e.shiftKey && this.checked) {
const current = checkboxes.indexOf(e.target)
const firstChecked = getFirstChecked()
const lastChecked = getLastChecked()
if (firstChecked < current) {
check(firstChecked, current)
} else if (current < lastChecked) {
check(current, lastChecked)
}
}
}
checkboxes.forEach(c => c.addEventListener('click', shiftSelect))