|
103 | 103 | </div> |
104 | 104 | </div> |
105 | 105 |
|
106 | | -<script> |
107 | | -const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]'); |
108 | | - |
109 | | -let lastChecked; |
110 | | - |
111 | | -function handleCheck(e) { |
112 | | - // Check if they had the shift key down |
113 | | - // AND check that they are checking it |
114 | | - let inBetween = false; |
115 | | - if (e.shiftKey && this.checked) { |
116 | | - // go ahead and do what we please |
117 | | - // loop over every single checkbox |
118 | | - checkboxes.forEach(checkbox => { |
119 | | - console.log(checkbox); |
120 | | - if (checkbox === this || checkbox === lastChecked) { |
121 | | - inBetween = !inBetween; |
122 | | - console.log('STarting to check them inbetween!'); |
123 | | - } |
| 106 | + <script> |
| 107 | + const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]'); |
| 108 | + |
| 109 | + let lastChecked; // track what was the last checked checkbox |
| 110 | + |
| 111 | + function handleCheck(e) { |
| 112 | + // console.log(e); |
| 113 | + let inBetween = false; |
| 114 | + if (e.shiftKey && this.checked) { |
| 115 | + // if the shift key is down and a checkbox is checked (not unchecked) do this: |
| 116 | + // loop over every checkbox |
| 117 | + checkboxes.forEach(checkbox => { |
| 118 | + console.log(checkbox); |
| 119 | + |
| 120 | + // is the checkbox equal to this or last checked? |
| 121 | + if (checkbox === this || checkbox === lastChecked) { |
| 122 | + inBetween = !inBetween; |
| 123 | + console.log('Start checking boxes in between...') |
| 124 | + } |
| 125 | + |
| 126 | + // target checkboxes between the first and last checked |
| 127 | + if (inBetween) { |
| 128 | + checkbox.checked = true; |
| 129 | + } |
| 130 | + |
| 131 | + }) |
124 | 132 |
|
125 | | - if (inBetween) { |
126 | | - checkbox.checked = true; |
127 | 133 | } |
128 | | - }); |
129 | | - } |
130 | 134 |
|
131 | | - lastChecked = this; |
132 | | -} |
| 135 | + lastChecked = this; |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + // Loop through getting all checkboxes with a class of .checkbox |
| 140 | + // Listen for checkboxes to be clicked |
| 141 | + checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck)); |
| 142 | + </script> |
133 | 143 |
|
134 | | -checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck)); |
135 | | -</script> |
136 | 144 | </body> |
137 | 145 | </html> |
0 commit comments