Skip to content

Commit 5528aeb

Browse files
committed
ten complete
1 parent dea85fb commit 5528aeb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

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

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

106106
<script>
107+
const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]')
108+
109+
let lastChecked;
110+
111+
function handleCheck(e){
112+
113+
let inBetween = false;
114+
if (e.shiftKey && this.checked){
115+
//loop over all checkboxes
116+
//see which was first
117+
//see which is last
118+
checkboxes.forEach(checkbox => {
119+
console.log(checkbox);
120+
if(checkbox === this || checkbox === lastChecked) {
121+
inBetween = !inBetween;
122+
//inBetween is switched right when first is checked
123+
}
124+
125+
if (inBetween){
126+
checkbox.checked = true;
127+
}
128+
//inBetween is a flag variable
129+
});
130+
}
131+
lastChecked = this;
132+
}
133+
134+
135+
136+
checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));
137+
107138
</script>
108139
</body>
109140
</html>

0 commit comments

Comments
 (0)