views:

223

answers:

6

Hi Guys,

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!

+1  A: 

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

thephpdeveloper
alright, that works in all browsers accept in firefox... any idea why?
n00b
navigationDiv.style.MozUserSelect="none" fixed it in firefox
n00b
A: 

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

 $('.someStuff').bind('dblclick', function() { return false; });
gnarf
A: 

Cancel the ondblclick event:

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

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

Andy E
copy: alright, that works in all browsers accept in firefox... any idea why?
n00b
Check my answer why....`return false` doesn't work for Mozilla engines. Rather, stop the event from executing instead of the function.
The Elite Gentleman
+1  A: 
$("yourselector").dblclick(function(){
    return false;
});

You can also use event.preventDefault()

$("yourselector").dblclick(function(event){
    event.preventDefault();
});
rahul
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
+2  A: 

Hey.

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
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
True. I wouldn't use this myself for that very reason, but it *is* an option if all else fails.
Atli
sweet, navigationDiv.style.MozUserSelect="none" in combination with returning false, works now in all browsers! thank you!
n00b
A: 

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.

The Elite Gentleman