Skip to content

Commit 0b8cae9

Browse files
committed
Day 10 Video solution
event.shiftKey so I didn’t need to listen for keyup/down use NodeList.forEach and assume order is by index?
1 parent 2fc59fe commit 0b8cae9

1 file changed

Lines changed: 27 additions & 38 deletions

File tree

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

Lines changed: 27 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -110,53 +110,42 @@
110110
const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]');
111111

112112
let lastChecked;
113-
let atEdge = false;
114-
let filling = false;
115-
let shiftClick = false;
116113

117114
function handleCheck(e) {
118-
if(lastChecked && shiftClick) {
119-
for( i = 0; i < checkboxes.length; i++ ) {
120-
atEdge = (checkboxes[i] == lastChecked || checkboxes[i] == e.currentTarget);
121-
filling = atEdge ? !filling : filling;
122-
if ( !atEdge ) {
123-
checkboxes[i].checked = filling;
115+
// Check if they had the shift key down
116+
// AND check that they are checking it
117+
let inBetween = false;
118+
if(e.shiftKey && this.checked) {
119+
//go ahead and do what we please
120+
//loop over every single checkbox
121+
checkboxes.forEach(checkbox => {
122+
if(checkbox === this || checkbox === lastChecked) {
123+
inBetween = !inBetween;
124+
}
125+
126+
if(inBetween) {
127+
checkbox.checked = true;
128+
}
129+
})
130+
}
131+
132+
if(e.shiftKey && lastChecked) {
133+
checkboxes.forEach(checkbox => {
134+
if (checkbox === this || checkbox === lastChecked) {
135+
inBetween = !inBetween;
136+
}
137+
138+
if(inBetween) {
139+
checkbox.checked = true;
124140
}
125-
}
141+
});
126142
}
127-
atEdge = false;
128-
filling = false;
129-
lastChecked = e.currentTarget;
143+
lastChecked = this;
130144
}
131145

132146
//add a click event listener on each item
133147
checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));
134148

135-
//figure out how to index the items
136-
137-
//Get the index of the last clicked item
138-
139-
//Get the index of the current clicked item
140-
141-
//Check all boxes in between last clicked and current click index if shift key down + click
142-
143-
144-
//Day 01 listening for keyboard events
145-
//Figure out how to tell if it's a shift+click
146-
function keydown(e) {
147-
if(e.keyCode == "16") {
148-
shiftClick = true;
149-
}
150-
}
151-
152-
function keyup(e) {
153-
if(e.keyCode == "16") {
154-
shiftClick = false;
155-
}
156-
}
157-
158-
window.addEventListener('keydown', keydown);
159-
window.addEventListener('keyup', keyup);
160149
</script>
161150
</body>
162151
</html>

0 commit comments

Comments
 (0)