Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript: Disable text selection via doubleclick

When double-clicking on a html page most browsers select the word you double-click on (or the paragraph you triple-click on). Is there a way to get rid of this behavior?

Note that I do not want to disable regular selection via single-click+dragging; i.e. jQuery UI's $('body').disableSelection() and the document.onselectstart DOM event are not what I want.

like image 748
ThiefMaster Avatar asked Nov 07 '10 11:11

ThiefMaster


People also ask

How do I turn off double click highlighting?

To disable the text selection when the user double clicks on the HTML element, you can use the user-select property and set its value to none in CSS.

How do I turn off text selection?

You can use the user-select property to disable text selection of an element. In web browsers, if you double-click on some text it will be selected/highlighted. This property can be used to prevent this.

How do I double click in JavaScript?

The dblclick event generates an event on double click the element. The event fires when an element is clicked twice in a very short span of time. We can also use the JavaScript's addEventListener() method to fire the double click event. In HTML, we can use the ondblclick attribute to create a double click event.


2 Answers

Just put this on the css interested section

 -moz-user-select     : none;  -khtml-user-select   : none;  -webkit-user-select  : none;  -o-user-select       : none;  user-select          : none; 
like image 42
Maurizio Battaghini Avatar answered Sep 22 '22 06:09

Maurizio Battaghini


I fear you can't prevent the selection itself being "native behavior" of the browser, but you can clear the selection right after it's made:

<script type="text/javascript"> document.ondblclick = function(evt) {     if (window.getSelection)         window.getSelection().removeAllRanges();     else if (document.selection)         document.selection.empty(); } </script> 

Edit: to also prevent selecting whole paragraph by "triple click", here is the required code:

var _tripleClickTimer = 0; var _mouseDown = false;  document.onmousedown = function() {     _mouseDown = true; };  document.onmouseup = function() {     _mouseDown = false; };  document.ondblclick = function DoubleClick(evt) {     ClearSelection();     window.clearTimeout(_tripleClickTimer);      //handle triple click selecting whole paragraph     document.onclick = function() {         ClearSelection();     };      _tripleClickTimer = window.setTimeout(RemoveDocumentClick, 1000); };  function RemoveDocumentClick() {     if (!_mouseDown) {         document.onclick = null;          return true;     }      _tripleClickTimer = window.setTimeout(RemoveDocumentClick, 1000);     return false; }  function ClearSelection() {     if (window.getSelection)         window.getSelection().removeAllRanges();     else if (document.selection)         document.selection.empty(); }​ 

Live test case.

Should be cross browser, please report any browser where it's not working.

like image 190
Shadow Wizard Hates Omicron Avatar answered Sep 23 '22 06:09

Shadow Wizard Hates Omicron