|
58 | 58 | --> |
59 | 59 | <div class="inbox"> |
60 | 60 | <div class="item"> |
61 | | - <input type="checkbox"> |
| 61 | + <input type="checkbox" id="01"> |
62 | 62 | <p>This is an inbox layout.</p> |
63 | 63 | </div> |
64 | 64 | <div class="item"> |
65 | | - <input type="checkbox"> |
| 65 | + <input type="checkbox" id="02"> |
66 | 66 | <p>Check one item</p> |
67 | 67 | </div> |
68 | 68 | <div class="item"> |
69 | | - <input type="checkbox"> |
| 69 | + <input type="checkbox" id="03"> |
70 | 70 | <p>Hold down your Shift key</p> |
71 | 71 | </div> |
72 | 72 | <div class="item"> |
73 | | - <input type="checkbox"> |
| 73 | + <input type="checkbox" id="04"> |
74 | 74 | <p>Check a lower item</p> |
75 | 75 | </div> |
76 | 76 | <div class="item"> |
77 | | - <input type="checkbox"> |
| 77 | + <input type="checkbox" id="05"> |
78 | 78 | <p>Everything in between should also be set to checked</p> |
79 | 79 | </div> |
80 | 80 | <div class="item"> |
81 | | - <input type="checkbox"> |
| 81 | + <input type="checkbox" id="06"> |
82 | 82 | <p>Try to do it without any libraries</p> |
83 | 83 | </div> |
84 | 84 | <div class="item"> |
85 | | - <input type="checkbox"> |
| 85 | + <input type="checkbox" id="07"> |
86 | 86 | <p>Just regular JavaScript</p> |
87 | 87 | </div> |
88 | 88 | <div class="item"> |
89 | | - <input type="checkbox"> |
| 89 | + <input type="checkbox" id="08"> |
90 | 90 | <p>Good Luck!</p> |
91 | 91 | </div> |
92 | 92 | <div class="item"> |
93 | | - <input type="checkbox"> |
| 93 | + <input type="checkbox" id="09"> |
94 | 94 | <p>Don't forget to tweet your result!</p> |
95 | 95 | </div> |
96 | 96 | </div> |
97 | 97 |
|
98 | 98 | <script> |
| 99 | +const inputs = document.querySelectorAll("input"); |
| 100 | + |
| 101 | +const updateTasks = (event) => { |
| 102 | + let clickAction = false; |
| 103 | + let nbChecked = 0; |
| 104 | + |
| 105 | + if (event.shiftKey && event.target.checked) { |
| 106 | + // check if odd number of checkeed box |
| 107 | + inputs.forEach( (input) => { |
| 108 | + if (input.checked) { |
| 109 | + nbChecked ++; |
| 110 | + } |
| 111 | + }); |
| 112 | + if (nbChecked != 2) return; |
| 113 | + |
| 114 | + inputs.forEach( (input) => { |
| 115 | + if (input.checked) { |
| 116 | + clickAction = !clickAction; |
| 117 | + return; |
| 118 | + } |
| 119 | + if (clickAction) input.click(); |
| 120 | + }); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +inputs.forEach( (input) => input.addEventListener("click", updateTasks)); |
| 125 | + |
99 | 126 | </script> |
100 | 127 | </body> |
101 | 128 | </html> |
0 commit comments