Skip to content

Commit 572a89f

Browse files
Hold shift and check checkboxes
1 parent 6a63495 commit 572a89f

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,38 @@
9696
</div>
9797

9898
<script>
99+
const body = document.querySelector('body');
100+
const inputs = document.querySelectorAll('input[type="checkbox"]');
101+
let isShiftPressed = false;
102+
let lastChecked = null;
103+
104+
inputs.forEach(input => input.addEventListener('click', handleCheckbox));
105+
console.log(inputs);
106+
107+
function handleShift(e) {
108+
isShiftPressed = !isShiftPressed;
109+
}
110+
111+
function handleCheckbox(e) {
112+
console.log(this.checked);
113+
let inBetween = false;
114+
115+
if (isShiftPressed) {
116+
inputs.forEach(checkbox => {
117+
if (checkbox === lastChecked || checkbox === this) {
118+
inBetween = !inBetween;
119+
}
120+
121+
if (inBetween) {
122+
checkbox.checked = true;
123+
}
124+
})
125+
}
126+
127+
lastChecked = this;
128+
}
129+
130+
body.addEventListener('keydown', handleShift)
99131
</script>
100132
</body>
101133
</html>

0 commit comments

Comments
 (0)