Skip to content

Commit 761545f

Browse files
committed
learned and finished day10
1 parent 8eb455b commit 761545f

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

10 - Hold Shift and Check Checkboxes/index-START.html

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,33 @@
104104
</div>
105105

106106
<script>
107+
const boxes = document.querySelectorAll('.inbox input[type="checkbox"]');
108+
const boxArr = Array.from(boxes);
109+
110+
let lastChecked;
111+
let onOff = false;
112+
113+
function handleCheck(e){
114+
if (!lastChecked) lastChecked = this;
115+
onOff = lastChecked.checked ? true : false;
116+
117+
if(e.shiftKey){
118+
let start = boxArr.indexOf(this); // if press shift key and check a box, note this box as start box
119+
120+
// as the process of selecting boxes at same time is: click a box -> press shift key -> click another box
121+
// the lastChecked is the box which will be selected in first step [lastChecked - start]
122+
let end = boxArr.indexOf(lastChecked);
123+
124+
// slice(start,end(not include))
125+
boxArr.slice(Math.min(start,end),Math.max(start,end)+1).forEach(input => input.checked = onOff);
126+
}
127+
lastChecked = this;
128+
129+
130+
}
131+
132+
boxes.forEach(box => box.addEventListener('click',handleCheck));
133+
107134
</script>
108135
</body>
109136
</html>

0 commit comments

Comments
 (0)