Skip to content

Commit b1897a4

Browse files
Jonathan_AlordaJonathan_Alorda
authored andcommitted
Adds improvements to how this works, event listeners are more specified to the node, not the whole nodelist parent, and we are going to be able to select individual messages and a range, such as message 2, and range 5-8, without selecting 3,4.
1 parent 9944f79 commit b1897a4

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

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

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,13 @@
129129
// 4 - You also need to find out the SHIFT keycode.
130130
const isShiftPressed = (e => {
131131
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}`);
135136

136137
if (e.shiftKey) {
137-
138+
// Find which indeces of the inbox are selected:
138139
items.forEach( (item, index) => {
139140
console.log('is item checked?', index, item.children[0].checked);
140141
console.table(item);
@@ -147,22 +148,28 @@
147148
}
148149
});
149150
console.log('typeof:', typeof checkedList);
151+
console.log(`checkedList.length: ${checkedList.length}`);
152+
150153

151154
let min = checkedList[0];
152155
let max = checkedList[checkedList.length - 1];
153156
console.log(min, max);
154157

155158
const rangeToCheck = items.slice(min, max);
156-
console.log(`rangeToCheck: ${JSON.stringify(rangeToCheck)}`)
159+
// console.log(`rangeToCheck: ${JSON.stringify(rangeToCheck)}`)
160+
console.info(rangeToCheck)
157161
rangeToCheck.forEach( item => item.children[0].checked = true);
158162
}
159163
console.log(`checkedList: ${checkedList}`);
160164
return checkedList;
161165

162166
});
163167

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+
})
166173

167174

168175
</script>

0 commit comments

Comments
 (0)