383

I bind two event handlers on this link:

<a href='#' id='elm'>Show Alert</a>

JavaScript:

$(function()
{
  $('#elm').click(_f);
  $('#elm').mouseover(_m);
});

function _f(){alert('clicked');}
function _m(){alert('mouse over');}

Is there any way to get a list of all events bound on an element, in this case on element with id="elm"?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Praveen Prasad
  • 31,561
  • 18
  • 73
  • 106
  • this worked for me: Object.keys(window).filter(f=> f.startsWith("on")==true && document.getElementById("Any_Element_ID_or_Referance")[f] !=undefined) – garish Jul 03 '22 at 04:08

9 Answers9

544

In modern versions of jQuery, you would use the $._data method to find any events attached by jQuery to the element in question. Note, this is an internal-use only method:

// Bind up a couple of event handlers
$("#foo").on({
    click: function(){ alert("Hello") },
    mouseout: function(){ alert("World") }
});

// Lookup events for this particular Element
$._data( $("#foo")[0], "events" );

The result from $._data will be an object that contains both of the events we set (pictured below with the mouseout property expanded):

Console output for $._

Then in Chrome, you may right click the handler function and click "view function definition" to show you the exact spot where it is defined in your code.

MrLore
  • 3,759
  • 2
  • 28
  • 36
Sampson
  • 265,109
  • 74
  • 539
  • 565
  • 66
    This only works for elements bound through the jQuery helpers. – Alex Ciminian Feb 18 '12 at 18:02
  • 4
    @jammypeach I'm attempting your solution but still getting undefined returned for the selector. I used $('#elem').bind('click', function() {}); if that would make a difference. – Marcus Oct 19 '12 at 02:13
  • @marcus wierd, I can't get it working right either - keep getting undefined back with jQuery 1.8.2. – totallyNotLizards Oct 23 '12 at 08:08
  • 6
    @marcus ahhh, woops I missed something, sorry :) `$._data(element[0], ‘events’);` – totallyNotLizards Oct 23 '12 at 08:10
  • @jammypeach This variable "element" where is it defined ? – Dfr Oct 26 '12 at 17:24
  • @Dfr That's just a reference to your Element. For instance, `var element = document.getElementById("foo");` would be a reference to `
    Foo
    `.
    – Sampson Oct 26 '12 at 17:46
  • Anyway, to find list of valid events to a element which can be attached? – Mr_Green Apr 26 '13 at 15:18
  • @Mr_Green Not exactly. Due to the possibility of custom events, there isn't a finite number of events that can be handled for any given element. – Sampson Apr 26 '13 at 16:06
  • 2
    `TypeError: $._data is not a function` (function($){ $._data($('#edit-search-block-form--2'), "events"); })(jQuery); – Yuseferi Jul 31 '14 at 20:14
  • 1
    any solution available to find events that are not bounded using jquery? – Kira Sep 01 '14 at 11:53
  • "Note, this is an internal-use only method" <- Does this mean its use should be avoided? – Sam Kauffman Mar 02 '15 at 17:01
  • @SamKauffman Generally speaking, yes. Unless it's documented for public use, there is no guarantee that it will stick around, or maintain the same implementation, moving forward. Proceed cautiously :) – Sampson Mar 02 '15 at 17:15
  • 17
    These days one must use: $._data( $('#foo')[0] ).events – Donald T Apr 14 '15 at 20:44
  • also remember that the function body is in the .handler if you refer to it in console of dev tools you will see whole body of function – Jacek Pietal Mar 08 '16 at 13:38
  • 2
    I can't help but think "modern" will get more and more relative, and of course may eventually be out of date - can we get a version number? – Code Jockey Mar 28 '16 at 18:05
  • 7
    `$._data()` is used by JQuery internal. `$.data()` is the public method to user. And `$.data(element, 'events')` works fine. – Lin Du May 11 '17 at 03:54
88

General case:

  • Hit F12 to open Dev Tools
  • Click the Sources tab
  • On right-hand side, scroll down to Event Listener Breakpoints, and expand tree
  • Click on the events you want to listen for.
  • Interact with the target element, if they fire you will get a break point in the debugger

Similarly, you can:

  • right click on the target element -> select "Inspect element"
  • Scroll down on the right side of the dev frame, at the bottom is 'event listeners'.
  • Expand the tree to see what events are attached to the element. Not sure if this works for events that are handled through bubbling (I'm guessing not)
acdcjunior
  • 132,397
  • 37
  • 331
  • 304
Vali Shah
  • 1,246
  • 9
  • 12
  • 4
    I agree this is the preferred method and is a universal solution, versus relying on jQuery, which may or may not be available. – deadbabykitten Apr 23 '15 at 17:48
  • 4
    @dead umm... the question specifically refers to jQuery and uses jQuery in the example attachment - the answer should only be valid in the context of jQuery(?) – Code Jockey Mar 28 '16 at 18:06
  • 6
    It's helpful to understand answers in other contexts as well. Just because someone asks a specific question doesn't mean that the constrained answer they will receive is the best one available. Especially with jQuery, people tend to rely on it as a crutch. Understanding the underlying architecture is important. This answer shows that jQuery isn't even necessarily required. The question and example is too vague to know the usage and therefore, leaves open to interpretation what could be considered a valid answer. – deadbabykitten Mar 28 '16 at 23:07
12

I'm adding this for posterity; There's an easier way that doesn't involve writing more JS. Using the amazing firebug addon for firefox,

  1. Right click on the element and select 'Inspect element with Firebug'
  2. In the sidebar panels (shown in the screenshot), navigate to the events tab using the tiny > arrow
  3. The events tab shows the events and corresponding functions for each event
  4. The text next to it shows the function location

enter image description here

kakoma
  • 1,179
  • 13
  • 17
12

You can now simply get a list of event listeners bound to an object by using the javascript function getEventListeners().

For example type the following in the dev tools console:

// Get all event listners bound to the document object
getEventListeners(document);
ScottyG
  • 3,204
  • 3
  • 32
  • 42
  • 10
    I think that's not a native function and needs the following script/dependency: https://github.com/colxi/getEventListeners This should be added to the answer, as it's misleading otherwise. But thanks for getting me to that 'plugin'; looks nice. :) – Dennis98 Mar 18 '19 at 14:14
6

The jQuery Audit plugin plugin should let you do this through the normal Chrome Dev Tools. It's not perfect, but it should let you see the actual handler bound to the element/event and not just the generic jQuery handler.

JohnK
  • 6,865
  • 8
  • 49
  • 75
5

While this isn't exactly specific to jQuery selectors/objects, in FireFox Quantum 58.x, you can find event handlers on an element using the Dev tools:

  1. Right-click the element
  2. In the context menu, Click 'Inspect Element'
  3. If there is an 'ev' icon next to the element (yellow box), click on 'ev' icon
  4. Displays all events for that element and event handler

FF Quantum Dev Tools

Chris22
  • 1,973
  • 8
  • 37
  • 55
4

Note that events may be attached to the document itself rather than the element in question. In that case, you'll want to use:

$._data( $(document)[0], "events" );

And find the event with the correct selector:

enter image description here

And then look at the handler > [[FunctionLocation]]

enter image description here

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
2

I used something like this if($._data($("a.wine-item-link")[0]).events == null) { ... do something, pretty much bind their event handlers again } to check if my element is bound to any event. It will still say undefined (null) if you have unattached all your event handlers from that element. That is the reason why I am evaluating this in an if expression.

Adrian Liew
  • 83
  • 1
  • 9
1

When I pass a little complex DOM query to $._data like this: $._data($('#outerWrap .innerWrap ul li:last a'), 'events') it throws undefined in the browser console.

So I had to use $._data on the parent div: $._data($('#outerWrap')[0], 'events') to see the events for the a tags. Here is a JSFiddle for the same: http://jsfiddle.net/giri_jeedigunta/MLcpT/4/

giri-jeedigunta
  • 191
  • 2
  • 6
  • 3
    The reason for this is that you are delegating the event from `$('#outerWrap')`. The events are actually bound to that element rather than the individual anchors. – Scottux May 14 '14 at 15:06