Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent text selection after double click

I'm handling the dblclick event on a span in my web app. A side-effect is that the double click selects text on the page. How can I prevent this selection from happening?

like image 818
David Avatar asked May 19 '09 00:05

David


People also ask

How do I stop my text from highlighting?

Select the text that you want to remove highlighting from, or press Ctrl+A to select all of the text in the document. Go to Home and select the arrow next to Text Highlight Color. Select No Color.


2 Answers

function clearSelection() {     if(document.selection && document.selection.empty) {         document.selection.empty();     } else if(window.getSelection) {         var sel = window.getSelection();         sel.removeAllRanges();     } } 

You can also apply these styles to the span for all non-IE browsers and IE10:

span.no_selection {     user-select: none; /* standard syntax */     -webkit-user-select: none; /* webkit (safari, chrome) browsers */     -moz-user-select: none; /* mozilla browsers */     -khtml-user-select: none; /* webkit (konqueror) browsers */     -ms-user-select: none; /* IE10+ */ } 
like image 76
Paolo Bergantino Avatar answered Sep 28 '22 19:09

Paolo Bergantino


In plain javascript:

element.addEventListener('mousedown', function(e){ e.preventDefault(); }, false); 

Or with jQuery:

jQuery(element).mousedown(function(e){ e.preventDefault(); }); 
like image 25
Tom Avatar answered Sep 28 '22 18:09

Tom