Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fixed bugs
fix #1: shift click a checkbox only check itself instead of checking the rest of the checkboxes if there isn't a last checkbox already
fix #2: unchecking a checkbox doesn't assign the checkbox to the lastChecked variable
fix #3: when selecting multiple checkboxes in the opposite direction of the last multiple checkboxes selection, the new selection is made from the lastCheckbox variable of the previous selection instead of the current selection. (e.g: selecting checkbox5 and selecting checkbox9 will select checkbox5,6,7,8,9, after that selecting checkbox1 will only select checkbox1,2,3,4,5 and uncheck 6,7,8,9 instead of checking 1 all the way to 9)
fix #4: shift-click on an already checked checkbox uncheck the checkbox instead checking the rest of the checkboxes
  • Loading branch information
Hieu authored Dec 20, 2016
commit 67517589446efee21d7db1737037c52091aa282b
18 changes: 14 additions & 4 deletions 10 - Hold Shift and Check Checkboxes/index-FINISHED.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@
</div>

<script>
const checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]');
var checkboxes = document.querySelectorAll('.inbox input[type="checkbox"]');

let lastChecked;
let lastChecked = 'none';

function handleCheck(e) {
// Check if they had the shift key down
// AND check that they are checking it
let inBetween = false;
if (e.shiftKey && this.checked) {
if (e.shiftKey && lastChecked != 'none' && this !== lastChecked) {
// go ahead and do what we please
// loop over every single checkbox
checkboxes.forEach(checkbox => {
Expand All @@ -124,11 +124,21 @@

if (inBetween) {
checkbox.checked = true;
}else{
if(checkbox !== this && checkbox !== lastChecked){
checkbox.checked = false;
}
}
});
}

lastChecked = this;
if(e.shiftKey && lastChecked != 'none'){
return;
}else if(this.checked == true){
lastChecked = this;
}else{
lastChecked = 'none';
}
}

checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));
Expand Down