Skip to content

Commit 69e49d8

Browse files
author
Brad Stayte
committed
finished 10
1 parent 03579b1 commit 69e49d8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

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

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

106106
<script>
107+
const checkboxes = Array.from(document.querySelectorAll('.inbox [type="checkbox"]'));
108+
109+
let previousChecked;
110+
111+
function handleCheck(e) {
112+
113+
if (e.shiftKey && this.checked && previousChecked) {
114+
let lower = Number(previousChecked.dataset.index);
115+
let upper = Number(this.dataset.index);
116+
117+
if (previousChecked.dataset.index > this.dataset.index) {
118+
119+
const temp = lower;
120+
lower = upper;
121+
upper = temp;
122+
}
123+
124+
const toUnCheck = [...checkboxes.slice(0, lower),...checkboxes.slice(upper+1)];
125+
checkboxes.forEach(x => x.checked = toUnCheck.indexOf(x) === -1);
126+
};
127+
128+
previousChecked = this;
129+
}
130+
131+
checkboxes.forEach((checkbox, index) => {
132+
checkbox.dataset.index=index;
133+
checkbox.addEventListener('click', handleCheck);
134+
});
135+
107136
</script>
108137
</body>
109138
</html>

0 commit comments

Comments
 (0)