disable text highlighting on double click in jQuery

JavascriptJqueryText

Javascript Problem Overview


I have this jQuery toggle. It work fine.

   <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 when I do fast clicking, it highlighted the text in it. Is there a way to stop the text from being highlighted?

Javascript Solutions


Solution 1 - Javascript

I'm writing on iPhone, while away from the desk, but a quick Google turned up this page: http://chris-barr.com/entry/disable_text_selection_with_jquery/">disable text selection with jQuery.


Edited in response to the 'dead link' comment (from @Herb Caudill). While the original link is, indeed, dead, it appears to be due to a site restructuring (rather than removal) and the new location for the article can be found here: http://chris-barr.com/index.php/entry/disable_text_selection_with_jquery/

And the code provided in that 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 snippet written by Chris Barr, of chris-barr.com, as accessed on Friday, 21st of January, 2011.

Solution 2 - Javascript

I solved this using the non-standard CSS keyword user-select:

.unselectable {
  -moz-user-select: none;
  -webkit-user-select: none;
  -ms-user-select: none;
}

Solution 3 - Javascript

If you use jQuery UI you can disable text selection as simple as that:

$("body").disableSelection();

Solution 4 - Javascript

//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);

http://bytes.com/topic/javascript/answers/635488-prevent-text-selection-after-double-click">Source</a>

Solution 5 - Javascript

I had the same problem and this worked for me:

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

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

P.S. This only disables the "visual effect", the text still gets selected. I hope that's why this got downvoted, because if your problem is only esthetical this solution works 100%.

Solution 6 - Javascript

To work with jQuery version above 1.9.x

HTML

The 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 preventDefault() instead of return false which doesn't kill any other handlers you might have

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

Example: jsFiddle

Solution 7 - Javascript

window.getSelection().empty()

works fine in Chrome, albeit with a quick flash of the selection, which is ugly.

Solution 8 - Javascript

$( 'selector' ).on( 'selectstart', 'selector', function( ) { 
  return false; 
}).css( 'MozUserSelect','none' ).mousedown( function( ) {
  return false;
}); 

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

Solution 9 - Javascript

document.onselectstart = function() { return false; }
document.onmousedown = function() { return false; }

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
QuestionReigelView Question on Stackoverflow
Solution 1 - JavascriptDavid ThomasView Answer on Stackoverflow
Solution 2 - JavascriptSjoerdView Answer on Stackoverflow
Solution 3 - JavascriptVisioNView Answer on Stackoverflow
Solution 4 - JavascriptAlex BagnoliniView Answer on Stackoverflow
Solution 5 - JavascriptnonzaprejView Answer on Stackoverflow
Solution 6 - JavascriptLahmizzarView Answer on Stackoverflow
Solution 7 - JavascripttimView Answer on Stackoverflow
Solution 8 - JavascriptpelisterView Answer on Stackoverflow
Solution 9 - JavascriptMike FurlenderView Answer on Stackoverflow