Created
September 18, 2022 11:14
-
-
Save benwoodward/d10b157ca8387df83ce57238a00909db to your computer and use it in GitHub Desktop.
shift select logic for checkboxes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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