File tree Expand file tree Collapse file tree 2 files changed +34
-0
lines changed
10 - Hold Shift and Check Checkboxes Expand file tree Collapse file tree 2 files changed +34
-0
lines changed Original file line number Diff line number Diff line change 1+ Things learned:
2+ - use flag to keep track of checked status
3+
4+ I did it differently by finding the index of the first and second checked items, then used forEach to change everything in between to checked.
Original file line number Diff line number Diff line change 9696 </ div >
9797
9898< script >
99+ let checkboxes = document . querySelectorAll ( '.item input' ) ;
100+ let arr = Array . from ( checkboxes ) ;
101+
102+ let lastChecked , first , last ;
103+
104+ function handleCheck ( event ) {
105+ first = arr . indexOf ( lastChecked )
106+ if ( event . shiftKey && this . checked ) {
107+ last = arr . indexOf ( this ) ;
108+ }
109+ arr . slice ( first , last + 1 ) . forEach ( item => item . checked = true ) ;
110+
111+ lastChecked = this ;
112+ /*
113+ let inBetween = false;
114+ if (event.shiftKey && this.checked) {
115+ checkboxes.forEach(item => {
116+ if(item === this || item === lastChecked) {
117+ inBetween = !inBetween;
118+ }
119+ if(inBetween) {
120+ item.checked = true;
121+ }
122+ })
123+ }
124+ */
125+ }
126+
127+ checkboxes . forEach ( item => item . addEventListener ( 'click' , handleCheck ) )
128+
99129</ script >
100130</ body >
101131</ html >
You can’t perform that action at this time.
0 commit comments