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;});
4
  • 1
    As a side note, you probably want to use e.preventDefault() instead of return false. Commented Apr 17, 2012 at 3:16
  • It's not supposed to be a solution, it's just something that you should know and do. Commented Apr 17, 2012 at 3:33
  • stackoverflow.com/questions/880512/… Commented Aug 12, 2013 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); Commented Nov 4, 2013 at 8:22

2 Answers 2

21

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/

Sign up to request clarification or add additional context in comments.

3 Comments

Note that this will disable all selection methods, not just double clicking.
@AndrewMarshall Thanks Mr. Marshall :)
@hguser which version of ie? 6/7?
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.

3 Comments

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...
Oh,My mistake. I thought it can prevent the dbl-select and select. It works. Thank you.
would you like something that prevents select as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.