|
129 | 129 | // 4 - You also need to find out the SHIFT keycode. |
130 | 130 | const isShiftPressed = (e => { |
131 | 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?'); |
| 132 | + console.info('e.target', e.target); |
| 133 | + |
| 134 | + const lastIndexClicked = items.indexOf(e.target.parentNode); |
| 135 | + console.log(`index of last clicked ${lastIndexClicked}`); |
135 | 136 |
|
136 | 137 | if (e.shiftKey) { |
137 | | - |
| 138 | + // Find which indeces of the inbox are selected: |
138 | 139 | items.forEach( (item, index) => { |
139 | 140 | console.log('is item checked?', index, item.children[0].checked); |
140 | 141 | console.table(item); |
|
147 | 148 | } |
148 | 149 | }); |
149 | 150 | console.log('typeof:', typeof checkedList); |
| 151 | + console.log(`checkedList.length: ${checkedList.length}`); |
| 152 | + |
150 | 153 |
|
151 | 154 | let min = checkedList[0]; |
152 | 155 | let max = checkedList[checkedList.length - 1]; |
153 | 156 | console.log(min, max); |
154 | 157 |
|
155 | 158 | const rangeToCheck = items.slice(min, max); |
156 | | - console.log(`rangeToCheck: ${JSON.stringify(rangeToCheck)}`) |
| 159 | + // console.log(`rangeToCheck: ${JSON.stringify(rangeToCheck)}`) |
| 160 | + console.info(rangeToCheck) |
157 | 161 | rangeToCheck.forEach( item => item.children[0].checked = true); |
158 | 162 | } |
159 | 163 | console.log(`checkedList: ${checkedList}`); |
160 | 164 | return checkedList; |
161 | 165 |
|
162 | 166 | }); |
163 | 167 |
|
164 | | - inbox.addEventListener('click', isShiftPressed); |
165 | | - |
| 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 | + }) |
166 | 173 |
|
167 | 174 |
|
168 | 175 | </script> |
|
0 commit comments