Skip to content

Instantly share code, notes, and snippets.

@benwoodward
Created September 18, 2022 11:14
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 benwoodward/d10b157ca8387df83ce57238a00909db to your computer and use it in GitHub Desktop.
Save benwoodward/d10b157ca8387df83ce57238a00909db to your computer and use it in GitHub Desktop.
shift select logic for checkboxes
const checkboxes = document.querySelectorAll('.checkbox input[type="checkbox"]');
let lastChecked;
function handleCheck(e) {
let inBetween = false;
if(e.shiftKey && this.checked) {
checkboxes.forEach( checkbox => {
if(checkbox === this || checkbox === lastChecked) {
inBetween = !inBetween;
}
if(inBetween) {
checkbox.checked = true;
}
});
}
lastChecked = this;
};
checkboxes.forEach(checkbox => checkbox.addEventListener('click', handleCheck));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment