Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
jQuery does not reliably trigger() events added with addEventListener() #2476
jQuery 2.x still has code from 1.x around that attempts to trigger native/custom browser (not the events added with
As jQuery 2.x does not care about APIs from the stone age any more, it would be great if you instead triggered
I should mention we encountered this issue when we tried to marry JSON Editor with jQuery Mobile on a select element. It turns out jQuery Mobile manipulates the select DOM node, then calls
Well, we still support IE8 in the
Although if you send us a PR with the code that works for all supported browsers, i think we can consider it.
It's been long enough that it's worth re-discussing here in the Github tracker, but I'm still of the opinion from trac-11047 that jQuery's event system is a layer on top of the DOM event system. For the exceptions where we end up triggering native events via DOM methods like
Reaching down to the level below us is pretty complicated. When you call
If the DOM
jQuery doesn't know whether there are native DOM handlers attached to an element because there is no DOM
We'd still want to support triggering an event handler with additional data, as the jQuery API currently documents that. Perhaps you could say that didn't happen for native events, but if the native event caused the bubbling you still need that data to be passed to jQuery handlers attached to that element.
Normally the response would be to make this a plugin, but given how tricky this is I'm not sure a generalized solution can be crafted inside or outside jQuery. I'm going to assume that the anonymous person there tried some experiments and came to the same conclusion, but if someone wants to give it another try we could look at it.
Out of interest, wouldn't it be viable to switch the whole event propagation mechanism to use the browsers native facilities to make this kind of interoperability just work? I.e. all events are registered via .addEventListener() and all .trigger() does is dispatch a real event? Sure this wouldn't work for old browsers, but thats what the compatibility branch is for. No?
As for jQuery specific functionality, that should be possible quite easily by wrapping the handlers registered via jQuery in a closure that extracts the extra information from the triggered custom event and hands it in as a parameter?
@dwt I can't know that it doesn't work, I haven't tried to write the code. There are a lot of potential issues but perhaps you can work them out. At least if you give it a try it will provide a double-check on my suspicion that it is not possible without significant breakage of the current APIs.
The lack of a DOM
Edit: So in short, an ideal patch for this would not be significantly bigger than the current code and not introduce any breakage to the current API contracts.