File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
10 - Hold Shift and Check Checkboxes Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change 104104 </ div >
105105
106106< script >
107+ const checkboxes = document . querySelectorAll ( 'input' ) ;
108+ let lastCheck = 0 ;
109+ let shifted = false ;
110+
111+ function clickEm ( ) {
112+ const checkArray = Array . from ( checkboxes ) ;
113+ const thisIndex = checkArray . indexOf ( this ) ;
114+
115+ if ( shifted ) {
116+ const subset = checkArray . slice ( lastCheck , thisIndex + 1 ) ;
117+ subset . forEach ( box => box . checked = this . checked ) ;
118+ }
119+
120+ lastCheck = thisIndex ;
121+ }
122+
123+ document . body . addEventListener ( 'keydown' , ( e ) => {
124+ if ( e . key === 'Shift' ) {
125+ shifted = true ;
126+ }
127+ } ) ;
128+ document . body . addEventListener ( 'keyup' , ( ) => shifted = false ) ;
129+ checkboxes . forEach ( checkbox => checkbox . addEventListener ( 'click' , clickEm ) ) ;
107130</ script >
108131</ body >
109132</ html >
You can’t perform that action at this time.
0 commit comments