Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9944f79

Browse files
Jonathan_AlordaJonathan_Alorda
Jonathan_Alorda
authored and
Jonathan_Alorda
committed
This is now loosely working, but I'd like to make it smarter. Assignment 'passed', however.
1 parent 10625b7 commit 9944f79

File tree

1 file changed

+45
-4
lines changed

1 file changed

+45
-4
lines changed

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

Lines changed: 45 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,21 +106,62 @@
106106
<script>
107107
// Let's find the .inbox element with JavaScript and see what it offers us:
108108
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
109110
console.table(inbox);
111+
console.table(items);
110112

111113
// .inbox is a div with nine children, each of them is a div.item
112114
// Let's look at one child, and check it:
113-
inbox.children[2].children[0].checked = true; // this line checks the third checkbox using JavaScript
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.
114117

115118
// The logic is, if you click index 2, and then shift+click on another index, write some code that:
116-
// 1 - Knows which index was clicked
119+
// 1 - Knows which indeces are checked
117120
// 2 - Once you have a start/end, you can use JavaScript to check all the remaining boxes.
118121
// 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.
119122
// 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
120128

121129
// 4 - You also need to find out the SHIFT keycode.
122-
const isShiftPressed = (e => console.log(`Was the SHIFT key pressed while you cliked? ${e.shiftKey}`));
123-
document.addEventListener('click', isShiftPressed);
130+
const isShiftPressed = (e => {
131+
const checkedList = [];
132+
// console.log(`Was the SHIFT key pressed while you clicked? ${e.shiftKey}`)
133+
console.info(e.target);
134+
// console.log('testing?');
135+
136+
if (e.shiftKey) {
137+
138+
items.forEach( (item, index) => {
139+
console.log('is item checked?', index, item.children[0].checked);
140+
console.table(item);
141+
142+
// Find out which checkboxes are checked
143+
if (item.children[0].checked) {
144+
console.log('*** ITEM IS CHECKED', index, item.children[0].checked);
145+
checkedList.push(index);
146+
console.log(`checkedList: ${checkedList}`);
147+
}
148+
});
149+
console.log('typeof:', typeof checkedList);
150+
151+
let min = checkedList[0];
152+
let max = checkedList[checkedList.length - 1];
153+
console.log(min, max);
154+
155+
const rangeToCheck = items.slice(min, max);
156+
console.log(`rangeToCheck: ${JSON.stringify(rangeToCheck)}`)
157+
rangeToCheck.forEach( item => item.children[0].checked = true);
158+
}
159+
console.log(`checkedList: ${checkedList}`);
160+
return checkedList;
161+
162+
});
163+
164+
inbox.addEventListener('click', isShiftPressed);
124165

125166

126167

0 commit comments

Comments
 (0)