Skip to content

Commit eefd1df

Browse files
author
mochibot
committed
tutorial 10 completed
1 parent c5a0b1b commit eefd1df

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Things learned:
2+
- use flag to keep track of checked status
3+
4+
I did it differently by finding the index of the first and second checked items, then used forEach to change everything in between to checked.

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,36 @@
9696
</div>
9797

9898
<script>
99+
let checkboxes = document.querySelectorAll('.item input');
100+
let arr = Array.from(checkboxes);
101+
102+
let lastChecked, first, last;
103+
104+
function handleCheck(event) {
105+
first = arr.indexOf(lastChecked)
106+
if (event.shiftKey && this.checked) {
107+
last = arr.indexOf(this);
108+
}
109+
arr.slice(first, last + 1).forEach(item => item.checked = true);
110+
111+
lastChecked = this;
112+
/*
113+
let inBetween = false;
114+
if (event.shiftKey && this.checked) {
115+
checkboxes.forEach(item => {
116+
if(item === this || item === lastChecked) {
117+
inBetween = !inBetween;
118+
}
119+
if(inBetween) {
120+
item.checked = true;
121+
}
122+
})
123+
}
124+
*/
125+
}
126+
127+
checkboxes.forEach(item => item.addEventListener('click', handleCheck))
128+
99129
</script>
100130
</body>
101131
</html>

0 commit comments

Comments
 (0)