3

i'm working on a slide gallery at the moment. My problem is that when i click the navigation divs very fast, the browsers default behavior is fired (selection of content).

My question is: how can I suppress the default double click behavior?

navigationDiv.onclick = function () {
  // do something
};

A jQuery solution would also be suitable since i'm using it.

Thanks in advance!

n00b
  • 16,088
  • 21
  • 56
  • 72

9 Answers9

7
$("yourselector").dblclick(function(){
    return false;
});

You can also use event.preventDefault()

$("yourselector").dblclick(function(event){
    event.preventDefault();
});
rahul
  • 184,426
  • 49
  • 232
  • 263
  • 11
    I can't seem to get this working. The event is firing OK, but neither the *return false* nor the *event.preventDefault()* statements are actually stopping Firefox from double-click-selecting the content. – Atli Mar 11 '10 at 11:47
4

Just FYI, in Firefox and Safari/Chrome, selection can be disabled through CSS, too:

.navigationDiv {
    -moz-user-select: none;             
    -webkit-user-select: none;  
}

I think this is more simple when your purpose is only to prevent selections from taking place on a certain element.

rinopo
  • 41
  • 1
3

You could try disabling the onselectstart event of the target element.

navigationDiv.onselectstart=function(){return false}

Not sure if this is x-browser compatible. (I'll check it)

Edit
Turns out that this is a IE-only event. To accomplish the same in Mozilla, you would have to disable the -moz-user-select CSS style. In JavaScript that would be:

navigationDiv.style.MozUserSelect="none"

To be honest, I think you would be better off disabling the double-click event, as described in the other comments here.

Atli
  • 7,855
  • 2
  • 30
  • 43
  • 2
    The only problem with this is if a user actually wants to select something using click drag highlighting, you cancel that action too. – Andy E Mar 11 '10 at 11:17
  • True. I wouldn't use this myself for that very reason, but it *is* an option if all else fails. – Atli Mar 11 '10 at 11:20
  • sweet, navigationDiv.style.MozUserSelect="none" in combination with returning false, works now in all browsers! thank you! – n00b Mar 11 '10 at 11:49
1

Putting a return false; at the end of the event handler should be able to suppress the default selection behaviour.

mauris
  • 42,982
  • 15
  • 99
  • 131
1

Calling event.preventDefault() in dblclick handler will help only if it is called in the element BEING CLICKED, not in the parent's event handler.

In scenarios when there's a container with multiple children, default behaviour of double click will happen in a child and bubbled up to it's parent. In this case there're 2 options:

  1. Clear selection in parent's event handler, e.g.: window.getSelection().empty(). The issue with this approach is that you'll notice blinking of the selected text and in Opera there'll be a pop-up ('Copy', 'Search').
  2. Traverse all children and call e.PreventDefault() in their dblclick.
Maxim Saplin
  • 4,115
  • 38
  • 29
0

You could try making the "dblclick" event return false?

 $('.someStuff').bind('dblclick', function() { return false; });
gnarf
  • 105,192
  • 25
  • 127
  • 161
0

Cancel the ondblclick event:

navigationDiv.ondblclick = function () { 
  return false;  // cancel default
}; 

http://www.quirksmode.org/js/events_mouse.html

Andy E
  • 338,112
  • 86
  • 474
  • 445
  • copy: alright, that works in all browsers accept in firefox... any idea why? – n00b Mar 11 '10 at 11:44
  • Check my answer why....`return false` doesn't work for Mozilla engines. Rather, stop the event from executing instead of the function. – Buhake Sindi Mar 11 '10 at 14:49
0

Quite Simple:

navigationDiv.ondblclick = function (event) {
  // do something
  if (!event) event = window.event;
        if (event.preventDefault) 
            event.preventDefault();
        else 
            event.returnValue = false;

};

This works on IE and Mozilla browsers.

Buhake Sindi
  • 87,898
  • 29
  • 167
  • 228
0

Neither e.preventDefault() nor return false ensure in all cases, that double-click's default action (text selection) will not happen. But, having read this note and this answer I've composed a working solution for a similar problem of my own:

$(...your selector...)
    .on('mousedown',function(e){ if (e.detail>1) return false }) // prevent dblclick default
    .on('dblclick', ...your dblclick handler...);
it-alien
  • 408
  • 5
  • 8