Skip to content

Commit d4aa1bc

Browse files
author
Kyle Bradshaw
committed
shift+checkboxes v1 working-ugly kb
1 parent 06095ff commit d4aa1bc

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

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

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

106106
<script>
107+
108+
let clickIndex = null;
109+
const inputs = document.querySelectorAll('input');
110+
111+
Array.from(inputs).forEach((input) => {
112+
input.addEventListener('click', (e) => {
113+
// console.log(e, 'click');
114+
if(e.target.checked === true) {
115+
if(e.shiftKey === true) {
116+
toggleShiftInputs();
117+
} else {
118+
findFocus();
119+
}
120+
} else {
121+
clickIndex = null;
122+
}
123+
});
124+
});
125+
126+
function findFocus() {
127+
Array.from(inputs).forEach((input, index, array) => {
128+
if(input === document.activeElement) {
129+
clickIndex = index;
130+
}
131+
});
132+
}
133+
134+
135+
function toggleShiftInputs() {
136+
let alpha = clickIndex;
137+
findFocus();
138+
let omega = clickIndex;
139+
140+
if(alpha !== null && omega !== null) {
141+
if(alpha > omega) { //5, 1
142+
//step through descending
143+
Array.from(inputs).forEach((input, index) => {
144+
if(index <= alpha && index >= omega ) {
145+
input.checked = true;
146+
}
147+
});
148+
} else {
149+
// step through ascending
150+
Array.from(inputs).forEach((input, index) => {
151+
if(index >= alpha && index <= omega ) {
152+
input.checked = true;
153+
}
154+
});
155+
}
156+
}
157+
}
158+
107159
</script>
108160
</body>
109161
</html>

0 commit comments

Comments
 (0)