37

When double-clicking on a html page most browsers select the word you double-click on (or the paragraph you triple-click on). Is there a way to get rid of this behavior?

Note that I do not want to disable regular selection via single-click+dragging; i.e. jQuery UI's $('body').disableSelection() and the document.onselectstart DOM event are not what I want.

iconoclast
  • 21,213
  • 15
  • 102
  • 138
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
  • 14
    Not my decision. The guy I'm coding for wants it like that. Probably because the page is part of a browsergame and some buttons (divs with click events) which increase/decrease values attract users to doubleclick as that's faster than singleclicking them many times. And if that selects stuff it's annoying and looks odd. – ThiefMaster Nov 07 '10 at 11:41
  • http://stackoverflow.com/questions/880512/prevent-text-selection-after-double-click – Aristoteles Aug 12 '13 at 14:53

5 Answers5

40

I fear you can't prevent the selection itself being "native behavior" of the browser, 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>

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

var _tripleClickTimer = 0;
var _mouseDown = false;

document.onmousedown = function() {
    _mouseDown = true;
};

document.onmouseup = function() {
    _mouseDown = false;
};

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

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

    _tripleClickTimer = window.setTimeout(RemoveDocumentClick, 1000);
};

function RemoveDocumentClick() {
    if (!_mouseDown) {
        document.onclick = null; 
        return true;
    }

    _tripleClickTimer = window.setTimeout(RemoveDocumentClick, 1000);
    return false;
}

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

Live test case.

Should be cross browser, please report any browser where it's not working.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
  • Thanks, works fine. Even though I've used an anonymous function instead of giving it a name - I don't see much sense in that as it's immediately assigned and unlikely to be used somewhere else. – ThiefMaster Nov 07 '10 at 11:46
  • Cheers, I agree with you about anonymous function, giving name to everything is just old (bad?) habit of mine. :/ – Shadow The GPT Wizard Nov 07 '10 at 12:09
  • but it's a habit that helps debuggers know the name of the function when you are looking at the stack, instead of seeing a bunch of anonymous. FF is much better at guessing function names, IE shows anonymous for most of my stacks – Ruan Mendes Dec 03 '10 at 00:28
  • @Juan thanks, good point.. when I have to debug I usually use alerts though so it doesn't really matter. :-) – Shadow The GPT Wizard Dec 04 '10 at 16:54
  • sorry for bringing up this old post, but this code doesn't work for triple clicks, where the browser usually selects the paragraph. Any solutions for that? – Sherzod Feb 09 '12 at 20:34
  • @shershams care to explain what is triple click? Isn't it just double click then one ordinary click? Anyway, no event for such thing so I fear not much can be done. – Shadow The GPT Wizard Feb 10 '12 at 12:18
  • @ShadowWizard: well, for example when you click once, you set the focus on the text, when you click twice it will select the word, and when you click 3 times, it will select the whole paragraph. In my case, I have a button, which can be clicked several times within short period of time, and since it's just a button, it does select text around on each third click, then when clicked again, the selection disappears, and this process repeats as user keeps clicking on the button. Any solutions for it? – Sherzod Feb 10 '12 at 18:10
  • @shershams I see now. See my edit - not sure about your button though, feel free to update my fiddle sample to demonstrate and I'll see what's going on. – Shadow The GPT Wizard Feb 12 '12 at 09:14
  • the highlight still "flashes" as you keep clicking on it... no worries, I guess I will just go with it :) +1 for your help. – Sherzod Feb 12 '12 at 17:32
  • @shershams yep it's due to the nature of the code, working only after the selection is already made. – Shadow The GPT Wizard Feb 12 '12 at 23:29
  • For those triple clicks you could `setTimeout(ClearSelection, 25);`. Depending on how fast your users usually click you might have to tweak the timeout a bit. – ThiefMaster Mar 29 '12 at 12:01
  • @ThiefMaster thanks, thanks for the edit too, wrote it before knowing about anonymous functions and didn't notice later. – Shadow The GPT Wizard Mar 29 '12 at 12:35
  • @ShadowWizard Still a slight bug with triple click (ff 15, Windows 7) - if I double click, then click once more but keep the button down for more than a second, the paragraph will still select ... – Joe Bloggs Aug 22 '12 at 15:55
  • @Joe good catch! Bug busted and fixed. Saw and fixed this in Chrome, should be fixed for all browsers though, using pretty standard solution of adding a flag to check the mouse button is released before clearing the document onclick event. :) – Shadow The GPT Wizard Aug 22 '12 at 18:37
  • This is not only glitchy, but also gives a terrible user experience. So many sites also abuse this to prevent copying text, rather than preventing blue selection on clickable elements. Downvoting for humanity sake. – SleepyCal Sep 29 '14 at 19:32
  • 1
    @sleepycal sorry about your terrible user experience but you should blame the site owners and/or customers insisting on such features, not shooting the messenger. – Shadow The GPT Wizard Sep 29 '14 at 20:02
12

The following works for me in the current Chrome (v56), Firefox (v51) and MS Edge (v38) browsers.

var test = document.getElementById('test');
test.addEventListener('mousedown', function(e){
    if (e.detail > 1){
        e.preventDefault();
    }
});
<p id="test">This is a test area</p>

The MouseEvent.detail property keeps track of the current click count which can be used to determine whether the event is a double, tripple, or more click.

Internet explorer unfortunately does not reset the counter after a timeout period so instead of getting a count of burst-clicks you get a count of how many times the user has clicked since the page was loaded.

kicken
  • 2,122
  • 14
  • 17
  • 2
    Not sure if this suddenly became possible only recently, but this is the only answer that provides a perfect solution. – Kirk Woll Oct 09 '17 at 01:54
9

Just put this on the css interested section

 -moz-user-select     : none;
 -khtml-user-select   : none;
 -webkit-user-select  : none;
 -o-user-select       : none;
 user-select          : none;
5

If you really want to disable selection on double-click and not just remove the selection afterwards (looks ugly to me), you need to return false in the second mousedown event (ondblclick won't work because the selection is made onmousedown).

**If somebody wants no selection at all, the best solution is to use CSS user-select : none; like Maurizio Battaghini proposed.

// set to true if you want to disable also the triple click event
// works in Chrome, always disabled in IE11
var disable_triple_click = true;

// set a global var to save the timestamp of the last mousedown
var down = new Date().getTime();
var old_down = down;

jQuery(document).ready(function($)
{
    $('#demo').on('mousedown', function(e)
    {
        var time = new Date().getTime();

        if((time - down) < 500 && 
        (disable_triple_click || (down - old_down) > 500))
        {
            old_down = down;
            down = time;

            e.preventDefault(); // just in case
            return false; // mandatory
        }

        old_down = down;
        down = time;
    });
});

Live demo here

Important notice: I set the sensitivity to 500ms but Double-click sensitivity (maximum time between clicks that is detected as a double click) can vary by operating system and browser, and is often user-configurable. - api.jquery.com

Tested in Chrome and IE11.

pmrotule
  • 9,065
  • 4
  • 50
  • 58
-1

Just to throw this out there, but here's the code I slap into my pages where I expect users to be clicking rapidly. However, this will also disable standard click-n-drag text selection.

document.body.onselectstart = function() {
    return false;
}
document.body.style.MozUserSelect = "none";
if (navigator.userAgent.toLowerCase().indexOf("opera") != -1) {
    document.body.onmousedown = function() {
        return false;
    }
}

Seems to work well for me.

MarthyM
  • 1,839
  • 2
  • 21
  • 23
Duncan
  • 1,530
  • 15
  • 20