File tree Expand file tree Collapse file tree 1 file changed +22
-0
lines changed
10 - Hold Shift and Check Checkboxes Expand file tree Collapse file tree 1 file changed +22
-0
lines changed Original file line number Diff line number Diff line change 104104 </ div >
105105
106106< script >
107+ let isShift = false ; // whether or not the shift key is currently pressed
108+ let lastCheckedElement = undefined ; // last element that was 'checked' by user
109+
110+ // Toggle flags for shift key
111+ document . addEventListener ( 'keydown' , e => isShift = e . shiftKey ) ;
112+ document . addEventListener ( 'keyup' , e => isShift = e . shiftKey ) ;
113+
114+ // grab all the checkboxes on the page
115+ const checkBoxes = Array . from ( document . querySelectorAll ( 'input[type="checkbox"]' ) ) ;
116+
117+ checkBoxes . forEach ( input => {
118+ input . addEventListener ( 'change' , e => {
119+ if ( isShift ) {
120+ const i1 = checkBoxes . findIndex ( c => c === lastCheckedElement ) ;
121+ const i2 = checkBoxes . findIndex ( c => c === e . target ) ;
122+ const startSlice = Math . min ( i1 , i2 ) + 1 ;
123+ const endSlice = Math . max ( i1 , i2 ) ;
124+ checkBoxes . slice ( startSlice , endSlice ) . forEach ( check => check . checked = true ) ;
125+ }
126+ lastCheckedElement = e . target ;
127+ } ) ;
128+ } ) ;
107129</ script >
108130</ body >
109131</ html >
You can’t perform that action at this time.
0 commit comments