Created
June 10, 2018 11:54
-
-
Save barsbek/30da6d6cccf78228569c2af59410b86a to your computer and use it in GitHub Desktop.
check multiple checkboxes while shift key pressed
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('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