Skip to content

Instantly share code, notes, and snippets.

@barsbek
Created June 10, 2018 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barsbek/30da6d6cccf78228569c2af59410b86a to your computer and use it in GitHub Desktop.
Save barsbek/30da6d6cccf78228569c2af59410b86a to your computer and use it in GitHub Desktop.
check multiple checkboxes while shift key pressed
const checkboxes = document.querySelectorAll('input[type="checkbox"]');
checkboxes.forEach(c => c.addEventListener('click', handleCheck));
let lastChecked = null;
function handleCheck(e) {
let inBetween = false;
if(e.shiftKey && this !== lastChecked) {
checkboxes.forEach(c => {
const isEdge = c === this || c == lastChecked;
if(isEdge) { inBetween = !inBetween }
if(inBetween || isEdge) { c.checked = this.checked }
})
}
lastChecked = this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment