Disable double-click text selection in jQuery

I have a jQuery switch. It works great.

<ul> <li>Go to the store</li> <li>Pick up dinner</li> <li>Debug crash</li> <li>Take a jog</li> </ul> 

  $("li").toggle( function () { $(this).css({"list-style-type":"disc", "color":"blue"}); }, function () { $(this).css({"list-style-type":"disc", "color":"red"}); }, function () { $(this).css({"list-style-type":"", "color":""}); } ); 

The problem is that when I make a quick click, it selects the text in it. Is there any way to stop text selection?

+50
javascript jquery text
Jan 25
source share
9 answers

I write on the iPhone, far from the table, but Google quickly raised this page: disable text selection using jQuery .




Edited in response to a dead link comment (by @Herb Caudill). Although the original link is really dead, apparently related to the restructuring of the site (and not to the deletion), and a new place for the article can be found here: http://chris-barr.com/index.php/entry/disable_text_selection_with_jquery/

And the code given in this article is reproduced below:

 $(function(){ $.extend($.fn.disableTextSelect = function() { return this.each(function(){ if($.browser.mozilla){//Firefox $(this).css('MozUserSelect','none'); }else if($.browser.msie){//IE $(this).bind('selectstart',function(){return false;}); }else{//Opera, etc. $(this).mousedown(function(){return false;}); } }); }); $('.noSelect').disableTextSelect();//No text selection on elements with a class of 'noSelect' }); 

jQuery fragment, written by Chris Barr, chris-barr.com , accessed on Friday, 21 st January 2011.

+52
Jan 25 '10 at
source share

If you are using jQuery UI, you can turn off text selection as easily:

 $("body").disableSelection(); 
+35
Apr 24 2018-12-12T00:
source share

I solved this using the custom CSS user-select keyword:

 .unselectable { -moz-user-select: none; -webkit-user-select: none; -ms-user-select: none; } 
+35
Mar 25 '13 at 15:39
source share
 //function to be reused later function clearSelection() { var sel ; if(document.selection && document.selection.empty){ document.selection.empty() ; } else if(window.getSelection) { sel=window.getSelection(); if(sel && sel.removeAllRanges) sel.removeAllRanges() ; } } $('p').click(clearSelection); 

Source

+10
Jan 25 '10 at 12:36
source share

I had the same problem and it worked for me:

 li.noselection::selection { background-color: transparent; } 

I tested it on Chrome, Firefox, EDGE and IE from 7 to 10.

PS This only disables the "visual effect", the text is still highlighted. I hope that’s why they voted for it, because if your problem is only aesthetic, this solution works 100%.

+7
May 13 '15 at 10:10
source share

To work with jQuery version above 1.9.x

HTML

HTML markup as shown above:

 <ul> <li>Go to the store</li> <li>Pick up dinner</li> <li>Debug crash</li> <li>Take a jog</li> </ul> 

Js

Use the preventDefault () function instead of return false, which does not kill other handlers that might be

 $('li').on('selectstart', function (event) { event.preventDefault(); }); 

Example: jsFiddle

+3
Oct 21 '13 at 23:42 on
source share
 window.getSelection().empty() 

works great in Chrome, albeit with a fast flash of highlight, which is ugly.

0
Aug 02 '11 at 23:20
source share
 $( 'selector' ).on( 'selectstart', 'selector', function( ) { return false; }).css( 'MozUserSelect','none' ).mousedown( function( ) { return false; }); 

replace the selector with your own - this code works fine in all browsers. Using .on () for elements dynamically inserted into the DOM

0
Mar 24 '13 at 7:58
source share
 document.onselectstart = function() { return false; } document.onmousedown = function() { return false; } 
0
Sep 09 '13 at 15:41
source share



All Articles