Created
January 13, 2017 12:29
-
-
Save danieluhl/50231ec1b254a6eed233e5f388cf73cc to your computer and use it in GitHub Desktop.
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 inputs = Array.from(document.querySelectorAll('input')); | |
inputs.forEach(input => input.addEventListener('click', handleClick)); | |
function clickBetween(first, last) { | |
const direction = first < last ? 1 : -1; | |
let index = first; | |
while (index !== last) { | |
index += direction; | |
inputs[index].checked = true; | |
} | |
} | |
let lastClicked = null; | |
function handleClick(e) { | |
const clicked = e.currentTarget; | |
if (clicked.checked) { | |
if (e.shiftKey) { | |
const clickedIndex = inputs.indexOf(clicked); | |
const lastClickedIndex = inputs.indexOf(lastClicked) | |
clickBetween(clickedIndex, lastClickedIndex); | |
} | |
lastClicked = clicked; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment