Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bf0652d

Browse files
#Javascript30 #10 - Hold shift and multiple checkboxes
1 parent 38f53ba commit bf0652d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

10 - Hold Shift and Check Checkboxes/index-START.html

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,27 @@
9999
</div>
100100

101101
<script>
102+
const checkboxes = document.querySelectorAll('.input input[type=checkbox]');
103+
let lastChecked;
104+
105+
function handleCheck (e) {
106+
let inBetween = false;
107+
if (e.shiftKey && this.checked) {
108+
checkboxes.forEach(checkbox => {
109+
if (checkbox === this || checkbox === lastChecked) {
110+
inBetween = !inBetween;
111+
}
112+
113+
if (inBetween) {
114+
checkbox.checked = true;
115+
}
116+
});
117+
}
118+
119+
lastChecked = this;
120+
}
121+
122+
checkboxes.forEach(checkbox => addEventListener('click', handleCheck));
102123
</script>
103124
</body>
104125
</html>

0 commit comments

Comments
 (0)