Skip to content

Commit a24be73

Browse files
committed
10th exercise done
1 parent 6c9eac2 commit a24be73

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

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

106106
<script>
107+
const checkboxes = document.querySelectorAll('input');
108+
let lastCheck = 0;
109+
let shifted = false;
110+
111+
function clickEm() {
112+
const checkArray = Array.from(checkboxes);
113+
const thisIndex = checkArray.indexOf(this);
114+
115+
if (shifted) {
116+
const subset = checkArray.slice(lastCheck, thisIndex + 1);
117+
subset.forEach(box => box.checked = this.checked);
118+
}
119+
120+
lastCheck = thisIndex;
121+
}
122+
123+
document.body.addEventListener('keydown', (e) => {
124+
if (e.key === 'Shift') {
125+
shifted = true;
126+
}
127+
});
128+
document.body.addEventListener('keyup', () => shifted = false);
129+
checkboxes.forEach(checkbox => checkbox.addEventListener('click', clickEm));
107130
</script>
108131
</body>
109132
</html>

0 commit comments

Comments
 (0)