Prevent mouse event from selecting text

I am collecting mouse events on a webpage, namely onclick. When I click in two different places, my code does something in those two places. But after the second click (with event.shiftKey = true), I also find that the page content between these two locations is selected as if I had done a click drag between the start and end points.

I looked at a couple of questions [1] and [2] regarding invalid text to be selected, but both of these answers are CSS related, which is unacceptable for this situation, because the elements involved are arbitrary and of different sizes, and because I don't want to disable showing the selection (I want to show if there is one!), I want to disable it from actually getting the selection at a certain time. Thus, eliminating any choice that might be "accidental" is also out of the question, as it will save you from any previous valid choice.

I thought the event somehow propagates into native code where it makes the selection, so I tried:

event.cancelBubble = true
event.preventDefault()
return false

      

But it didn't work. I've only tested this with Chrome, but it's best to use cross browser.

Update: I created one script to demonstrate the problem . Click on any line of text in the output. Then slide + click on any other line. Both lines will be applied in the CSS, but there is also a cyan-blue browser selection that I'm trying to prevent.

Update 2: I vote to close because this is pointed out by @ivy_lynx, it is indeed a duplicate and the answer to this related question solves the problem.

+3
javascript selection


source to share


No one has answered this question yet

See similar questions:

122
Is there a way to make the text unselected in the HTML page?
nine
How can I turn off text selection using shift without turning off all text selection?
five
how to prevent text selection when dragging the mouse in Firefox?
five
Is it possible to create a select object without any user interaction?

or similar:

7494
How can I remove a specific element from an array in JavaScript?
5722
How can I remove a property from a JavaScript object?
5129
How do I return a response from an asynchronous call?
2812
event.preventDefault () vs. return false
2414
How can I find out which radio button is selected using jQuery?
2201
How do you decide when to use Node.js?
2132
Get selected text from dropdown (select field) using jQuery
1966
How can I select an element with multiple classes in jQuery?
1678
Binding events to dynamically created elements?
1566
Get selected value in dropdown using JavaScript



All Articles
Loading...
X
Show
Funny
Dev
Pics