Skip to content

Commit 8e7afa4

Browse files
authored
Merge pull request #1 from alorda/lesson-10
Lesson 10 Into Master Branch
2 parents 97874ce + 63c1c4a commit 8e7afa4

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

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

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

106106
<script>
107+
// Let's find the .inbox element with JavaScript and see what it offers us:
108+
const inbox = document.querySelector('.inbox');
109+
const items = [].slice.call(document.querySelectorAll('.item')); // This returns a node list of all elements with the class of .item
110+
console.table(inbox);
111+
console.table(items);
112+
113+
// .inbox is a div with nine children, each of them is a div.item
114+
// Let's look at one child, and check it:
115+
// inbox.children[2].children[0].checked = true; // this line checks the third checkbox using JavaScript
116+
// items.forEach(item => item.children[0].checked = true); // This is how I can check every box with JavaScript. So we have to set a start/stop with this.
117+
118+
// The logic is, if you click index 2, and then shift+click on another index, write some code that:
119+
// 1 - Knows which indeces are checked
120+
// 2 - Once you have a start/end, you can use JavaScript to check all the remaining boxes.
121+
// 3 - If you check 5, then 2, boxes 2 through 5 should be checked. If you shift+click again on box 7, boxes 2 through 7 should be clicked.
122+
// So, the code needs to always look for the lowest checked value, highest checked value, and if you click below or above that, set a new low/high limit for what is checked.
123+
// Except, here is an edge case:
124+
// You click index 2.
125+
// You click index 7.
126+
// You then SHIFT+click index 4
127+
// Do you have to SHIFT+click only the second value in the range? Will the
128+
129+
// 4 - You also need to find out the SHIFT keycode.
130+
const isShiftPressed = (e => {
131+
const checkedList = [];
132+
console.info('e.target', e.target);
133+
134+
const lastIndexClicked = items.indexOf(e.target.parentNode);
135+
console.log(`index of last clicked ${lastIndexClicked}`);
136+
137+
if (e.shiftKey) {
138+
// Find which indeces of the inbox are selected:
139+
items.forEach( (item, index) => {
140+
console.log('is item checked?', index, item.children[0].checked);
141+
console.table(item);
142+
143+
// Find out which checkboxes are checked
144+
if (item.children[0].checked) {
145+
console.log('*** ITEM IS CHECKED', index, item.children[0].checked);
146+
checkedList.push(index);
147+
console.log(`checkedList: ${checkedList}`);
148+
}
149+
});
150+
console.log('typeof:', typeof checkedList);
151+
console.log(`checkedList.length: ${checkedList.length}`);
152+
153+
154+
let min = checkedList[0];
155+
let max = checkedList[checkedList.length - 1];
156+
console.log(min, max);
157+
158+
const rangeToCheck = items.slice(min, max);
159+
// console.log(`rangeToCheck: ${JSON.stringify(rangeToCheck)}`)
160+
console.info(rangeToCheck)
161+
rangeToCheck.forEach( item => item.children[0].checked = true);
162+
}
163+
console.log(`checkedList: ${checkedList}`);
164+
return checkedList;
165+
166+
});
167+
168+
// inbox.addEventListener('click', isShiftPressed);
169+
// Add the event listener to each item, instead of to the inbox; the reason is that if I held SHIFT and clicked on a paragraph, and didnt' select a checkbox, and didn't add any "if (checkedList.length < 0 )" logic, it was selecting every single checkbox. This is a more specific approach, otherwise, we can joke about "it's a feature, not a bug".
170+
items.forEach(item => {
171+
item.addEventListener('click', isShiftPressed);
172+
})
173+
174+
107175
</script>
108176
</body>
109177
</html>

0 commit comments

Comments
 (0)