This is a very old topic, however, I would like to blow off the dust and start a fresh discussion on the matter.
I will start with a story of a front end developer, lets call him Bob. One day Bob was wondering around in the magical jQuery Forrest when he had a nice little idea for a plugin. Bob wanted to create a simple/customizable way to ask a user for confirmation before the default events take place on an object.
Here is a real world scenario. We have a "Delete Button" ... it has a click event attached to it somewhere in the code base that triggers some "functions" and deletes some "stuff". To implement the "confirm before delete" functionality we could just track down what is being executed after the click and modify the code to wait on a response first.
But... Bob thinks... "What if we cant modify the code base or what if there are multiple events being triggered, they would also need to be modified to wait."
Then Bob has a light-bulb... "I can just get all the events associated with this element and move my event to the top of the list so it runs first."
So depending on the response of the confirm, it would trigger the rest of the events or cancel.
Bob, with this nice little solution in mind, goes to the mighty Google and asks... "How to get all event listeners bound to an element with jQuery"
Bob is shocked when his research shows that this is in fact not possible (anymore). As of 1.8 In jQuery the only way to access the events data is with...
- jQuery._data(element, "events")
It is not good news for Bob to have write his plugin using the undocumented internal data structure. Bob now needs to tell the users of his plugin that his script must come directly after jQuery in order for it's confirm click event to be bound first. Bob is not happy.
I think Bob's frustration stems from the fact that the data is available, its right there, but with it, comes the threat that this functionality could be chopped off in later versions, without warning.
from 2011 announcing 1.8 the following was said on this matter...
jQuery stores its event-related data in a data object named (wait for it)
on each element. This is an internal data structure so in 1.8 this will be removed from the user data name space so it won’t conflict with items of the same name. jQuery’s event data can still be accessed via
but be aware that this is an internal data structure that is undocumented and should not be modified.
I would like to recommend the following on Bob's behalf...
- Official Documented access to the list of all events attached to an object
- Ability to override the order of this list (irrespective of where/when the events are bound) and choose which events fire first using event namespaces for example
Any comments, thoughts or rants about this are more than welcome.
Look forward to hearing from you all.