How can I prevent text/element selection with cursor drag

JavascriptJquery

Javascript Problem Overview


I made a paging control and I noticed that while clicking on the buttons it is very easy to accidentally select the individual images and text. Is it possible to prevent this?

To clarify selecting I mean highlighting with the mouse. (Try dragging your mouse from one side of the screen to the other.)

If you try to highlight the text/controls in this grid it can't be selected. How is that done? Link

Javascript Solutions


Solution 1 - Javascript

dragging and selecting both initialize on a mouse down event and update on subsequent mouse moves. When you handle the events to begin dragging, or to follow the mouse, cancel the event's bubbling and override the default browser return:

something like this in your begin dragging mousedown and move handlers-

> e=e || window.event; > pauseEvent(e);

function pauseEvent(e){
    if(e.stopPropagation) e.stopPropagation();
    if(e.preventDefault) e.preventDefault();
    e.cancelBubble=true;
    e.returnValue=false;
    return false;
}

		

Solution 2 - Javascript

For dragging, you're capturing the mousedown and mousemove events. (And hopefully touchstart and touchmove events as well, to support touch interfaces.)

You'll need to call event.preventDefault() in both the down and move events in order to keep the browser from selecting text.

For example (using jQuery):

var mouseDown = false;
$(element).on('mousedown touchstart', function(event) {
  event.preventDefault();
  mouseDown = true;
});
$(element).on('mousemove touchmove', function(event) {
  event.preventDefault();
  if(mouseDown) {
    // Do something here.
  }
});
$(window.document).on('mouseup touchend', function(event) {
  // Capture this event anywhere in the document, since the mouse may leave our element while mouse is down and then the 'up' event will not fire within the element.
  mouseDown = false;
});

Solution 3 - Javascript

This is a very old post. It may not answer exactly that situation, but i use CSS for my solution:

-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

Solution 4 - Javascript

I wanted to comment, but i don't have enough reputation. Using the suggested function from @kennebec solved my problem in my javascript dragging library. It works flawlessy.

function pauseEvent(e){
    if(e.stopPropagation) e.stopPropagation();
    if(e.preventDefault) e.preventDefault();
    e.cancelBubble=true;
    e.returnValue=false;
    return false;
}

i called it in my mousedown and mousemove custom function, immediately after i can recognize i clicked on the right element. If i call it just on top of the function i just kill any click on the document. My function is registered as an event on document.body.

Solution 5 - Javascript

try this:

document.onselectstart = function()
{
    window.getSelection().removeAllRanges();
};

Solution 6 - Javascript

This can be achieved using CSS in most browsers and the unselectable expando in IE. See my answer here: https://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting/4358620#4358620

Solution 7 - Javascript

If you need to block text selection for a certain element using JavaScript, then the simplest method for me was to assign userSelect style like this:

var myElement = document.createElement('div');
myElement.style.userSelect = 'none';

Solution 8 - Javascript

simply prevent it by calling blur() function when selected as following :

 <input Value="test" onSelect="blur();">

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
QuestionThe Muffin ManView Question on Stackoverflow
Solution 1 - JavascriptkennebecView Answer on Stackoverflow
Solution 2 - JavascriptRobin DaughertyView Answer on Stackoverflow
Solution 3 - JavascriptMikeView Answer on Stackoverflow
Solution 4 - JavascriptAndreaBogazziView Answer on Stackoverflow
Solution 5 - JavascriptAlex PacurarView Answer on Stackoverflow
Solution 6 - JavascriptTim DownView Answer on Stackoverflow
Solution 7 - JavascriptErkki TeedlaView Answer on Stackoverflow
Solution 8 - JavascriptMehdiView Answer on Stackoverflow