var $lastSelected = [],
container = $('.container'),
collection = $('.selectable');
container.on('click', '.selectable', function (e) {
var that = $(this),
$selected,
direction;
if (e.shiftKey) {
if ($lastSelected.length > 0) {
if (that[0] == $lastSelected[0]) {
// The user has clicked on the same item, so do nothing.
// Remember, `that` is our event target
// You could also make this deselect everything except for this item
return false;
}
if ($lastSelected.parent().attr("id") == that.parent().attr("id")) {
direction = that.nextAll('.container .lastSelected').length > 0 ? 'forward' : 'back';
if ('forward' == direction) {
// Last selected is after the current selection
$selected = that.nextUntil($lastSelected, '.selectable');
} else {
// Last selected is before the current selection
$selected = $lastSelected.nextUntil(that, '.selectable');
}
//Add and remove classes as necessary
collection.removeClass('selected');
$selected.addClass('selected');
$lastSelected.addClass('selected');
that.addClass('selected');
} else {
$lastSelected = that;
// Mark our previously selected item
that.addClass('lastSelected');
collection.removeClass('selected');
that.addClass('selected');
}
} else {
$lastSelected = that;
// Mark our previously selected item
that.addClass('lastSelected');
collection.removeClass('selected');
that.addClass('selected');
}
} else {
// Not a shift select, so we'll just...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.