When placing a handler to an element via $.on(), is there any way to find the delegated event when all you have is the target?
EX:
$('body').on('click', '#myLink', function(){ doStuff() });
…
$('#myLink').eventsFromOn();
I know this seems odd, because if the event has the delegate event data, its not really a delegated event anymore, but its worth it to ask…
Thanks!
If you’re looking for handler data, looks like you can find it with undocumented information if you try hard enough. On the undocumented
eventsdata object, you have the keystype,handler, and — importantly —selector. So if you start with#mylink, you can look at theeventsdata at each level in its ancestry looking for a match for your desired event where$("#mylink").is(selector)is true.For example, with this HTML:
If I execute
Then do this:
I get this:
undefined No Properties Object click: Array[1] 0: Object data: undefined guid: 1 handler: function () { namespace: "" origType: "click" quick: Array[4] selector: "#mylink" type: "click" __proto__: Object delegateCount: 1 length: 1 __proto__: Array[0] __proto__: Object undefined No PropertiesAs you can see, the
clickhander onbodyis there, with aselectormatching the element#mylink.