disable text selection while pressing 'shift'

JavascriptJquery

Javascript Problem Overview


I'm implementing item selection functionality in javascript (using jQuery). item is a li that contains some html.
the user can click on one item (makes it selected) and then shift-click on another item to select all the items in between. this basically works OK, however the shift-click also selects (higtlights) the text that appears inside the items (the basic browser functionality for shift clicks). is there any way to disable this?

Javascript Solutions


Solution 1 - Javascript

Try a combo of JavaScript and css to prevent the selection in the first place:

$('li').attr('unselectable', 'on'); // IE

css (for browsers not IE):

li {
            user-select: none; /* CSS3 (little to no support) */
        -ms-user-select: none; /* IE 10+ */
       -moz-user-select: none; /* Gecko (Firefox) */
    -webkit-user-select: none; /* Webkit (Safari, Chrome) */
}

Solution 2 - Javascript

Try this after the Shift + click...

document.getSelection().removeAllRanges();

If that is not effective enough, you might have to also cancel the onselectstart event...

window.onload = function() {
  document.onselectstart = function() {
    return false;
  }
}

Solution 3 - Javascript

I have an application like that. Trick is, I wanted to allow selection, but I also wanted Ctrl-click and Shift-click for selecting items.

What I found was that everyone but IE allows you to beat this by canceling the mousedown event, and in IE what works best is to temporarily disable onselectstart:

$("#id").mousedown(function (e) {
    if (e.ctrlKey || e.shiftKey) {
        // For non-IE browsers
        e.preventDefault();

        // For IE
        if ($.support.msie) {
            this.onselectstart = function () { return false; };
            var me = this;  // capture in a closure
            window.setTimeout(function () { me.onselectstart = null; }, 0);
        }
    }
});

Solution 4 - Javascript

This code will stop text selection only during the "shift" key pressed, if you release it then you can select text as usual. Currently I'm using this es6 code.

["keyup","keydown"].forEach((event) => {
    window.addEventListener(event, (e) => {
        document.onselectstart = function() {
            return !(e.key == "Shift" && e.shiftKey);
        }
    });
});

Solution 5 - Javascript

If you have checkboxes in your table rows (items), you can simply set focus to the checkbox for the row once you've made the selection. Focusing on the checkbox should get rid of the browser selection. You can also focus on a textbox or other form element. For JQuery,

$('tr').bind('click', function(e) {
  // If shift key was down when the row was clicked
  if (e.shiftKey) { 
    // Make the row selected
    $(this).parent().children().slice($(last).index(), $(this).index()+1).addClass('selected').find('input:checkbox').attr('checked', true);
    
    // Now focus on the checkbox within the row
    $('input:checkbox', $(this)).focus();
  }
});

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAsaf DavidView Question on Stackoverflow
Solution 1 - JavascriptRoatin MarthView Answer on Stackoverflow
Solution 2 - JavascriptJosh StodolaView Answer on Stackoverflow
Solution 3 - JavascriptAnthony MillsView Answer on Stackoverflow
Solution 4 - JavascriptRubanraj RavichandranView Answer on Stackoverflow
Solution 5 - JavascriptRay PereaView Answer on Stackoverflow