Visitar URL original
challenge 10 solution #1 · rnaidenov/JavaScript30@2136ea9 · GitHub
Skip to content

Commit 2136ea9

Browse files
committed
challenge 10 solution wesbos#1
1 parent d3bed31 commit 2136ea9

1 file changed

Lines changed: 67 additions & 8 deletions

File tree

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

Lines changed: 67 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
<!DOCTYPE html>
22
<html lang="en">
3+
34
<head>
45
<meta charset="UTF-8">
56
<title>Hold Shift to Check Multiple Checkboxes</title>
67
</head>
8+
79
<body>
810
<style>
9-
1011
html {
1112
font-family: sans-serif;
1213
background: #ffc600;
@@ -17,7 +18,7 @@
1718
margin: 50px auto;
1819
background: white;
1920
border-radius: 5px;
20-
box-shadow: 10px 10px 0 rgba(0,0,0,0.1);
21+
box-shadow: 10px 10px 0 rgba(0, 0, 0, 0.1);
2122
}
2223

2324
.item {
@@ -30,7 +31,7 @@
3031
border-bottom: 0;
3132
}
3233

33-
input:checked + p {
34+
input:checked+p {
3435
background: #F9F9F9;
3536
text-decoration: line-through;
3637
}
@@ -44,13 +45,13 @@
4445
padding: 20px;
4546
transition: background 0.2s;
4647
flex: 1;
47-
font-family:'helvetica neue';
48+
font-family: 'helvetica neue';
4849
font-size: 20px;
4950
font-weight: 200;
5051
border-left: 1px solid #D1E2FF;
5152
}
5253
</style>
53-
<!--
54+
<!--
5455
The following is a common layout you would see in an email client.
5556
5657
When a user clicks a checkbox, holds Shift, and then clicks another checkbox a few rows down, all the checkboxes inbetween those two checkboxes should be checked.
@@ -95,7 +96,65 @@
9596
</div>
9697
</div>
9798

98-
<script>
99-
</script>
99+
<script>
100+
101+
102+
let lastIdxSelected = null;
103+
let shiftPressed = false;
104+
105+
106+
// Good to not forget:
107+
// keydown - when pressed
108+
// keyup - when releasd
109+
110+
// When shift is pressed
111+
document.addEventListener('keydown', e => {
112+
if (e.keyCode === 16) {
113+
shiftPressed = true;
114+
}
115+
})
116+
117+
118+
// When shift is released
119+
document.addEventListener('keyup', e => {
120+
if (e.keyCode === 16) {
121+
shiftPressed = false;
122+
lastIdxSelected = null;
123+
}
124+
})
125+
126+
127+
const items = Array.from(document.querySelectorAll('.item'));
128+
129+
items.forEach(item => item.addEventListener('click', e => {
130+
const currIdxSelected = getPressedIndex(e.currentTarget);
131+
// select multiple, if possible
132+
if(shiftPressed && lastIdxSelected){
133+
const from = lastIdxSelected > currIdxSelected ? currIdxSelected : lastIdxSelected;
134+
const to = lastIdxSelected > currIdxSelected ? lastIdxSelected : currIdxSelected;
135+
selectMultiple(from, to)
136+
}
137+
138+
lastIdxSelected = currIdxSelected;
139+
}));
140+
141+
142+
function selectMultiple(fromIdx, toIdx){
143+
for(let i=fromIdx; i <= toIdx; i++) {
144+
const item = items[i];
145+
const isChcked = item.querySelector('input').checked;
146+
item.querySelector('input').checked = true;
147+
}
148+
}
149+
150+
151+
function getPressedIndex(selected){
152+
return items.indexOf(selected);
153+
}
154+
155+
156+
157+
</script>
100158
</body>
101-
</html>
159+
160+
</html>

0 commit comments

Comments
 (0)