Skip to content

Commit 5f38a07

Browse files
authored
Update index-FINISHED.html
1 parent 5fdd6f2 commit 5f38a07

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

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

Lines changed: 34 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -103,35 +103,43 @@
103103
</div>
104104
</div>
105105

106-
<script>
107-
const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]');
108-
109-
let lastChecked;
110-
111-
function handleCheck(e) {
112-
// Check if they had the shift key down
113-
// AND check that they are checking it
114-
let inBetween = false;
115-
if (e.shiftKey && this.checked) {
116-
// go ahead and do what we please
117-
// loop over every single checkbox
118-
checkboxes.forEach(checkbox => {
119-
console.log(checkbox);
120-
if (checkbox === this || checkbox === lastChecked) {
121-
inBetween = !inBetween;
122-
console.log('STarting to check them inbetween!');
123-
}
106+
<script>
107+
const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]');
108+
109+
let lastChecked; // track what was the last checked checkbox
110+
111+
function handleCheck(e) {
112+
// console.log(e);
113+
let inBetween = false;
114+
if (e.shiftKey && this.checked) {
115+
// if the shift key is down and a checkbox is checked (not unchecked) do this:
116+
// loop over every checkbox
117+
checkboxes.forEach(checkbox => {
118+
console.log(checkbox);
119+
120+
// is the checkbox equal to this or last checked?
121+
if (checkbox === this || checkbox === lastChecked) {
122+
inBetween = !inBetween;
123+
console.log('Start checking boxes in between...')
124+
}
125+
126+
// target checkboxes between the first and last checked
127+
if (inBetween) {
128+
checkbox.checked = true;
129+
}
130+
131+
})
124132

125-
if (inBetween) {
126-
checkbox.checked = true;
127133
}
128-
});
129-
}
130134

131-
lastChecked = this;
132-
}
135+
lastChecked = this;
136+
137+
}
138+
139+
// Loop through getting all checkboxes with a class of .checkbox
140+
// Listen for checkboxes to be clicked
141+
checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));
142+
</script>
133143

134-
checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));
135-
</script>
136144
</body>
137145
</html>

0 commit comments

Comments
 (0)