|
106 | 106 | <script>
|
107 | 107 | // Let's find the .inbox element with JavaScript and see what it offers us:
|
108 | 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 |
109 | 110 | console.table(inbox);
|
| 111 | + console.table(items); |
110 | 112 |
|
111 | 113 | // .inbox is a div with nine children, each of them is a div.item
|
112 | 114 | // 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. |
114 | 117 |
|
115 | 118 | // 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 |
117 | 120 | // 2 - Once you have a start/end, you can use JavaScript to check all the remaining boxes.
|
118 | 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.
|
119 | 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 |
120 | 128 |
|
121 | 129 | // 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); |
124 | 165 |
|
125 | 166 |
|
126 | 167 |
|
|
0 commit comments