6060 <!--
6161 The following is a common layout you would see in an email client.
6262
63- When a user clicks a checkbox, holds Shift, and then clicks another checkbox a
63+ When a user clicks a checkbox, holds Shift, and then clicks another checkbox a
6464 few rows down, all the checkboxes inbetween those two checkboxes should be checked.
6565 -->
6666 < div class ="inbox ">
105105 < script >
106106 // Wrap JS code in an IIFE, creating a closure so as to not pollute the global namespace
107107 ( ( ) => {
108+
109+ // Declare variable that will be defined later as the input that the user
110+ // most recently selected
111+ let lastSelected ;
112+
108113 // Declare & define constant as a reference to all inputs of type checkbox
109114 // that are children of the 'inbox' class
110115 const checkBoxes = document . querySelectorAll ( '.inbox input[type="checkbox"]' ) ;
111- // Declare variable that will be defined later
112- let lastSelected ;
113116
114117 // Event Handler function
115- function multiCheck ( e ) {
118+ const multiCheck = ( el , e ) => {
116119
117120 // If the lastSelected variable is defined and the shift key was pressed
118121 // when this event triggered...
119122 if ( lastSelected && e . shiftKey ) {
120- // Set checked property of function context to be the same as the
123+
124+ // Set checked property of checkbox to be the same as the
121125 // checked property of the last selected input
122- this . checked = lastSelected . checked ;
123- // Declare two variables
124- let startIdx , endIdx ;
125- // If the ID of the function context is greater than the ID of the last selected input...
126- this . id > lastSelected . id ?
127- // ...define variable startIdx as last selected input ID and endIdx as function context ID
128- [ startIdx , endIdx ] = [ lastSelected . id , this . id ] :
126+ el . checked = lastSelected . checked ;
127+
128+ // If the ID of the checkbox is greater than the ID of the last selected input...
129+ const [ startIdx , endIdx ] = el . id > lastSelected . id ?
130+ // ...define variable startIdx as last selected input ID and endIdx as checkbox ID
131+ [ lastSelected . id , el . id ] :
129132 // ...if not, switch variable definitions
130- [ startIdx , endIdx ] = [ this . id , lastSelected . id ] ;
133+ [ el . id , lastSelected . id ] ;
131134
132135 // Declare & define constant as an array of inputs between the last selected input
133- // and the function context input
134- const middleInputs = Array . from ( checkBoxes ) . slice ( parseInt ( startIdx ) + 1 , endIdx ) ;
135- // Set checked property of each input to match checked property of function context input
136- middleInputs . forEach ( checkbox => checkbox . checked = this . checked ) ;
137- } // If no lastSelected or shiftKey was not pressed, entire if statement is skipped
136+ // and the checkbox input
137+ const middleInputs = [ ...checkBoxes ] . slice ( parseInt ( startIdx ) + 1 , endIdx ) ;
138+
139+ // Set checked property of each input to match checked property of checkbox input
140+ middleInputs . forEach ( checkbox => checkbox . checked = el . checked ) ;
141+ }
142+
143+ // If no lastSelected or shiftKey was not pressed, entire if statement is skipped
138144 // Define lastSelected as the input that was the checked which triggered the event handler
139- lastSelected = this ;
145+ lastSelected = el ;
140146 }
147+
141148 // Add event listener to each input that will call multiCheck function on the 'click' event
142- checkBoxes . forEach ( checkBox => checkBox . addEventListener ( 'click' , multiCheck ) ) ;
149+ checkBoxes . forEach ( checkBox => {
150+ checkBox . addEventListener ( 'click' , multiCheck . bind ( null , checkBox ) ) ;
151+ } ) ;
143152 } ) ( ) ;
144153 </ script >
145154</ body >
146155
147- </ html >
156+ </ html >
0 commit comments