How to prevent the double-click select text in Javascript

hguser picture hguser · Apr 17, 2012 · Viewed 7.5k times · Source

Say I have an ul (li) list in the page:

<ul>
 <li>xxx<li>
 <li>xxx<li>
</ul>

The element li are clickable and double-clickable, they are attached with these events, and I return false in both of them.

$('ul li').on('click',function(){
    //do what I want
    return false;
}).on('dblclick',function(){
    //do what I want
    return false;
});

But when the user double-clicks the element, the text inside the li will be selected. How can this be prevented?

Update:

Solved now,I use the following code with the css selector by NiftyDude:

$('ul li').on('click',function(){
    //do what I want
    return false;
}).....on('dragstart',function(){return false;}).on('selectstart',function(){return false;});

Answer

Andreas Wong picture Andreas Wong · Apr 17, 2012

You can disable text selection using css (Note that this will effectively disable all selection methods and not just double clicking)

ul li {
   -webkit-touch-callout: none;
   -webkit-user-select: none;
   -khtml-user-select: none;
   -moz-user-select: none;
   -ms-user-select: none;
   user-select: none;
}

http://jsfiddle.net/T3d7v/1/