Skip to content

Commit b74d6f3

Browse files
Adds finished code day 10.
1 parent b4035a5 commit b74d6f3

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

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

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

106106
<script>
107+
let isShift = false; // whether or not the shift key is currently pressed
108+
let lastCheckedElement = undefined; // last element that was 'checked' by user
109+
110+
// Toggle flags for shift key
111+
document.addEventListener('keydown', e => isShift = e.shiftKey);
112+
document.addEventListener('keyup', e => isShift = e.shiftKey);
113+
114+
// grab all the checkboxes on the page
115+
const checkBoxes = Array.from(document.querySelectorAll('input[type="checkbox"]'));
116+
117+
checkBoxes.forEach(input => {
118+
input.addEventListener('change', e => {
119+
if (isShift) {
120+
const i1 = checkBoxes.findIndex(c => c === lastCheckedElement);
121+
const i2 = checkBoxes.findIndex(c => c === e.target);
122+
const startSlice = Math.min(i1, i2) + 1;
123+
const endSlice = Math.max(i1, i2);
124+
checkBoxes.slice(startSlice, endSlice).forEach(check => check.checked = true);
125+
}
126+
lastCheckedElement = e.target;
127+
});
128+
});
107129
</script>
108130
</body>
109131
</html>

0 commit comments

Comments
 (0)