9

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;});
monkey
  • 526
  • 7
  • 21
hguser
  • 35,079
  • 54
  • 159
  • 293
  • 1
    As a side note, you probably want to use [`e.preventDefault()` instead of `return false`](http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false). – Andrew Marshall Apr 17 '12 at 03:16
  • It's not supposed to be a solution, it's just something that you should know and do. – Andrew Marshall Apr 17 '12 at 03:33
  • http://stackoverflow.com/questions/880512/prevent-text-selection-after-double-click – Aristoteles Aug 12 '13 at 14:53
  • 2
    `return false;` is the same as calling both `preventDefault()` and `stopPropagation()` on the event handler arg. Perhaps the best thing to do, though, would be to just pass `false` directly: `/*....*/.on('dragstart selectstart', false);` – AndrewF Nov 04 '13 at 08:22

2 Answers2

20

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/

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
3

You can't stop the select from happening 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>

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

var _tripleClickTimer = 0;
document.ondblclick = function(evt) {
    ClearSelection();
    window.clearTimeout(_tripleClickTimer);

    //handle triple click selecting whole paragraph
    document.onclick = function() {
        ClearSelection();
    };

    _tripleClickTimer = window.setTimeout(function() {
           document.onclick = null; 
    }, 1000);
};

function ClearSelection() {
    if (window.getSelection)
        window.getSelection().removeAllRanges();
    else if (document.selection)
        document.selection.empty();
}

Source/Live test.

This should work on any browser, please report any browser where it's not working.

grepsedawk
  • 3,324
  • 2
  • 26
  • 49
  • Yes, but then I would say you must re-word your question. If you notice, you are unable to select the test due to double (and triple) click... Which is EXACTLY what you wanted... – grepsedawk Apr 17 '12 at 23:15
  • Oh,My mistake. I thought it can prevent the dbl-select and select. It works. Thank you. – hguser Apr 18 '12 at 05:38
  • would you like something that prevents select as well? – grepsedawk Apr 18 '12 at 22:42