Here's a JSFiddle of the behavior I'm seeing, relating to middle-click and the click
event in Chrome and FF.
'click' kinda sorta works
Approach 1: Bind a click
handler directly to an a
element and a middle-click will trigger the handler in Chrome but not in FF.
$('div a').on('click', function(ev) {
// middle click triggers this handler
});
Approach 2: Bind a delegated click
handler to a div
which contains one or more a
. Middle click will not trigger this handler in Chrome or FF.
$('div').on('click', 'a', function(ev) {
// middle click doesn't trigger this handler
});
This approach is extremely valuable if the div starts out empty and the a
elements are filled in later by an AJAX call, or as a result of some user input.
'mouseup' works
Using mouseup
instead of click
causes both approach 1 and 2 to work in both browsers.
// Approach 1 w/ mouseup
$('div a').on('mouseup', function(ev) {
// middle click **does** trigger this handler in Chrome and FF
});
// Approach 2 w/ mouseup
$('div').on('mouseup', 'a', function(ev) {
// middle click **does** trigger this handler in Chrome and FF
});
Here's the JSFiddle with mouseup
.
This is interesting and might be useful in some cases, because mouseup
is almost click
. But mouseup
isn't click
, and I'm after the behavior of click
. I do not want to create a hacky mousedown; setTimeout; mouseup
simulation of click
.
I'm pretty sure the answer is "nope", but is there a cross-browser way to cause middle-click to trigger click
handlers? If not, what are the reasons why?
mousedown; setTimeout; mouseup
simulation ofclick
." Reinventing the click seems like a bad idea, but I'll weigh the pros and cons again. – Sassaby