Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting middle mouse click event jQuery

I want to show a jQuery-UI dialog box as a popup when user clicks on left mouse button or the middle one. It works for left click (I get the alert box and after that the popup) but doesn't work for middle (neither alert box nor popup). What am I missing?

$('a.external').live('click', function(e){
  if( e.which <= 2 ) {
    e.preventDefault();
    alert ("inside if");
  }
  popUp.start(this);
});
like image 264
Chankey Pathak Avatar asked Jun 16 '13 06:06

Chankey Pathak


People also ask

How to detect which mouse button is clicked using JavaScript event object?

Now when we talk about event objects there are two flavors of them, one is raw JavaScript Event Object and another is jQuery event object. If we are using JavaScript Event Object and the browser is IE 8 or lower versions then we use event.button property to detect which mouse button is clicked.

What is The mouseDown event in jQuery?

The mousedown event is sent when any mouse button is clicked. To act only on specific buttons, we can use the event object's which property. Not all browsers support this property (Internet Explorer uses button instead), but jQuery normalizes the property so that it is safe to use in any browser.

How do I get my Codes notified of mouse clicks using jQuery?

Getting my codes notified of mouse clicks is simple via jQuery: I wrap the document object with jQuery and supplied a function to the mousedown function. In the function, I will receive a event object e from jQuery when mouse clicks are detected. I then use e.which to switch between numbers that identify which mouse button was being clicked.

Can the middle and right buttons be detected by the browser?

While the middle and right buttons can be detected with these properties, this is not reliable. In Opera and Safari, for example, right mouse button clicks are not detectable by default. If the user clicks on an element, drags away from it, and releases the button, this is still counted as a mousedown event.


2 Answers

Use mousedown or mouseup instead of click. And (unless you are using a very old version of jQuery) use .on() instead of .live():

$(document).on("mousedown", "a.external", function(e) {
   if( e.which <= 2 ) {
      e.preventDefault();
      alert ("inside if");
   }
   popUp.start(this);
});

...where ideally you'd use a parent element much closer to the link than document.

Demo: http://jsfiddle.net/7S2SQ/

like image 127
nnnnnn Avatar answered Oct 30 '22 00:10

nnnnnn


To get this working fully in Firefox (40.0.3), I had to implement .on('mouseup', fn), as follows:

$(selector).on('mouseup', function (e) {

    switch (e.which)
    {
        // Left Click.
        case 1:
            // By way of example, I've added Ctrl+Click Modifiers.
            if (e.ctrlKey) {
                // Ctrl+LeftClick behaviour.
            } else {
                // Standard LeftClick behaviour.
            }
            break;

        // Middle click.
        case 2:
            // Interrupts "Firefox Open New Tab" behaviour.
            break;

        // Default behaviour for right click.
        case 3:
            return;
    }

    // Pass control back to default handler.
    return true;
});
like image 43
Apache Avatar answered Oct 30 '22 00:10

Apache