Skip to content

Instantly share code, notes, and snippets.

@zergius-eggstream
Created March 13, 2017 10:08
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zergius-eggstream/bea51020b471886bafe1b2baef9817b8 to your computer and use it in GitHub Desktop.
Save zergius-eggstream/bea51020b471886bafe1b2baef9817b8 to your computer and use it in GitHub Desktop.
jQuery plugin for shift + click to select multiple checkboxes (https://gist.github.com/AndrewRayCode/3784055 modified for live collections support)
// usage: $('.container').shiftSelectable() to select all checkboxes in container $('.cont')
// or $('.container').shiftSelectable({items: '.shift-selectable'}) to select only checkboxes with shift-selectable class
$.fn.shiftSelectable = function(config) {
config = $.extend({
items: 'input[type="checkbox"]'
}, config);
var $container = this;
var lastChecked;
$container.on('click', config.items, function(evt) {
if(!lastChecked) {
lastChecked = this;
return;
}
var $boxes = $container.find(config.items);
if(evt.shiftKey) {
var start = $boxes.index(this),
end = $boxes.index(lastChecked);
$boxes.slice(Math.min(start, end), Math.max(start, end) + 1)
.attr('checked', lastChecked.checked)
.trigger('change');
}
lastChecked = this;
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment