I have this neat script for handling when a user shift clicks from one checkbox to another to select multiples.
var last_clicked = null;
$("table.multi-select").on("click", "input:checkbox", function (e) {
// allow shift clicking
var grid = $(this).parents("table");
if (!last_clicked)
last_clicked = this;
if (event.shiftKey) {
var start = grid.find("tbody input:checkbox").index(this);
var end = grid.find("tbody input:checkbox").index(last_clicked);
for (i = Math.min(start, end) ; i <= Math.max(start, end) ; i++) {
grid.find("tbody input:checkbox")[i].checked = last_clicked.checked;
}
}
last_clicked = this;
});
It works well. I'm now replacing my checkboxes with styled bootstrap / FlatUI ones. These hide the actual checkbox and replace it with a styled label. I've modified my shift click script accordingly
$("table.multi-select").on("click", "label.checkbox", function (e) {
// allow shift clicking
var grid = $(this).parents("table");
if (!last_clicked)
last_clicked = this;
if (event.shiftKey) {
var start = grid.find("tbody label.checkbox").index(this);
var end = grid.find("tbody label.checkbox").index(last_clicked);
// find checked state of last clicked
var checked_last = $(last_clicked).hasClass("checked");
for (i = Math.min(start, end) ; i <= Math.max(start, end) ; i++) {
// don't check 'this', because flatUI will toggle it when we're done
if(i != start)
$(grid.find("tbody label.checkbox input:checkbox")[i]).checkbox(checked_last ? "check" : "uncheck");
}
}
last_clicked = this;
});
This works too. The problem is, it also selects all the text on the page between the checkboxes! How do I stop this from happening?