Skip to content

Commit ea7fc60

Browse files
committed
Finish challenge wesbos#9/wesbos#10
1 parent d7a6798 commit ea7fc60

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Hold Shift to Check Multiple Checkboxes</title>
6+
</head>
7+
<body>
8+
<style>
9+
10+
html {
11+
font-family: sans-serif;
12+
background:#ffc600;
13+
}
14+
15+
.inbox {
16+
max-width:400px;
17+
margin:50px auto;
18+
background:white;
19+
border-radius:5px;
20+
box-shadow:10px 10px 0 rgba(0,0,0,0.1);
21+
}
22+
23+
.item {
24+
display:flex;
25+
align-items:center;
26+
border-bottom: 1px solid #F1F1F1;
27+
}
28+
29+
.item:last-child {
30+
border-bottom:0;
31+
}
32+
33+
34+
input:checked + p {
35+
background:#F9F9F9;
36+
text-decoration: line-through;
37+
}
38+
39+
input[type="checkbox"] {
40+
margin:20px;
41+
}
42+
43+
p {
44+
margin:0;
45+
padding:20px;
46+
transition:background 0.2s;
47+
flex:1;
48+
font-family:'helvetica neue';
49+
font-size: 20px;
50+
font-weight: 200;
51+
border-left: 1px solid #D1E2FF;
52+
}
53+
54+
55+
</style>
56+
<!--
57+
The following is a common layout you would see in an email client.
58+
59+
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.
60+
61+
-->
62+
<div class="inbox">
63+
<div class="item">
64+
<input type="checkbox">
65+
<p>This is an inbox layout.</p>
66+
</div>
67+
<div class="item">
68+
<input type="checkbox">
69+
<p>Check one item</p>
70+
</div>
71+
<div class="item">
72+
<input type="checkbox">
73+
<p>Hold down your Shift key</p>
74+
</div>
75+
<div class="item">
76+
<input type="checkbox">
77+
<p>Check a lower item</p>
78+
</div>
79+
<div class="item">
80+
<input type="checkbox">
81+
<p>Everything inbetween should also be set to checked</p>
82+
</div>
83+
<div class="item">
84+
<input type="checkbox">
85+
<p>Try do it with out any libraries</p>
86+
</div>
87+
<div class="item">
88+
<input type="checkbox">
89+
<p>Just regular JavaScript</p>
90+
</div>
91+
<div class="item">
92+
<input type="checkbox">
93+
<p>Good Luck!</p>
94+
</div>
95+
<div class="item">
96+
<input type="checkbox">
97+
<p>Don't forget to tweet your result!</p>
98+
</div>
99+
</div>
100+
101+
<script>
102+
const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]');
103+
104+
let lastChecked;
105+
106+
function handleCheck(e) {
107+
let inBetween = false;
108+
109+
if (e.shiftKey && this.checked) {
110+
checkboxes.forEach(checkbox => {
111+
112+
if (checkbox === this || checkbox === lastChecked) {
113+
inBetween = !inBetween;
114+
}
115+
116+
if (inBetween) {
117+
console.info(checkbox);
118+
checkbox.checked = true;
119+
}
120+
});
121+
}
122+
123+
if (this.checked) {
124+
lastChecked = this;
125+
}
126+
}
127+
128+
checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));
129+
</script>
130+
</body>
131+
</html>

0 commit comments

Comments
 (0)