Skip to content

Commit 708bfe2

Browse files
committed
added solution for selecting multiple checkboxes
1 parent 7cc3b47 commit 708bfe2

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@
104104
</div>
105105

106106
<script>
107+
const checkboxes = document.querySelectorAll('input[type="checkbox"]');
108+
109+
let lastChecked;
110+
111+
function handleCheck(e){
112+
let inBetween = false;
113+
//check if shift key pressed
114+
//And check of they are checking the checkbox
115+
if (e.shiftKey && this.checked) {
116+
//loop over every checkbox
117+
118+
checkboxes.forEach(checkbox => {
119+
if (checkbox === this || checkbox === lastChecked) {
120+
inBetween = !inBetween;
121+
}
122+
123+
if (inBetween) checkbox.checked = true;
124+
});
125+
}
126+
127+
lastChecked = this;
128+
}
129+
130+
checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));
107131
</script>
108132
</body>
109133
</html>

0 commit comments

Comments
 (0)