Skip to content

Instantly share code, notes, and snippets.

@danieluhl
Created January 13, 2017 12:29
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 danieluhl/50231ec1b254a6eed233e5f388cf73cc to your computer and use it in GitHub Desktop.
Save danieluhl/50231ec1b254a6eed233e5f388cf73cc to your computer and use it in GitHub Desktop.
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