168

Lets suppose I've a link on my page:

<a href="#" id="foo">Click Here</a>

I don't know anything else, but when I click on the link, an alert("bar") is displayed. So I know that somewhere, some code is getting bound to #foo.

How can I find the code that is binding the alert("bar") to the click event? I'm looking for a solution with Chrome.

Ps.: The example is fictive, so I'm not looking for solution like: "Use XXXXXX and search the whole project for "alert(\"bar\")". I want a real debugging/tracing solution.

8 Answers 8

153

Using Chrome 15.0.865.0 dev. There's an "Event Listeners" section on the Elements panel:

enter image description here

And an "Event Listeners Breakpoints" on the Scripts panel. Use a Mouse -> click breakpoint and then "step into next function call" while keeping an eye on the call stack to see what userland function handles the event. Ideally, you'd replace the minified version of jQuery with an unminified one so that you don't have to step in all the time, and use step over when possible.

enter image description here

9
  • 11
    Getting closer, but most of the results in there are pointing to the line 16 of... jquery.min.js :( ( I understand why, no need to explain, but how can we find who called the bind() method of jQuery ?
    – FMaz008
    Commented Sep 7, 2011 at 18:03
  • Those tools are all available in Chrome 12.0.742.100 too. :) Thanks !
    – FMaz008
    Commented Sep 7, 2011 at 18:56
  • 13
    @Fluffy: You don't have to. Just klick the { } symbol in the left bottom corner when viewing js. Magic. Commented Mar 19, 2014 at 12:18
  • Stepping through jQuery's complex event dispatching code is a big pain. The jQuery Audit answer below (stackoverflow.com/a/30487583/24267) is so much better.
    – mhenry1384
    Commented Oct 26, 2015 at 20:04
  • 4
    To exclude jquery from the call stack, black box the script: developer.chrome.com/devtools/docs/blackboxing @IonuțG.Stan, or mods, can you update the answer with a reference to blackboxing -- seems to be a common question relevant to this answer. Commented Sep 26, 2016 at 15:30
52

You can also use Chrome's inspector to find attached events another way, as follows:

  1. Right click on the element to inspect, or find it in the 'Elements' pane.
  2. Then in the 'Event Listeners' tab/pane, expand the event (eg 'click')
  3. Expand the various sub-nodes to find the one you want, and then look for where the 'handler' sub-node is.
  4. Right click the word 'function', and then click 'Show function definition'

This will take you to where the handler was defined, as demonstrated in the following image, and explained by Paul Irish here: https://groups.google.com/forum/#!topic/google-chrome-developer-tools/NTcIS15uigA

'Show Function definition'

1
  • 1
    two years old, and still the best answer to this question.
    – Stuart
    Commented Apr 22, 2016 at 14:16
17

Give it a try to the jQuery Audit extension (https://chrome.google.com/webstore/detail/jquery-audit/dhhnpbajdcgdmbbcoakfhmfgmemlncjg), after installing follow these steps:

  1. Inspect the element
  2. On the new 'jQuery Audit' tab expand the Events property
  3. Choose for the Event you need
  4. From the handler property, right click over function and select 'Show function definition'
  5. You will now see the Event binding code
  6. Click on the 'Pretty print' button for a more readable view of the code
3
  • 1
    This is an excellent extension and saves loads of time sifting through JavaScript. Commented Nov 11, 2015 at 15:58
  • I often find that "Event Listeners" lists "No event listeners", and that selecting "Event listener breakpoints" > Mouse > Click does not create a breakpoint. This plugin works very well.
    – StuartN
    Commented Nov 23, 2015 at 18:05
  • @Javier > it is a great response. Does it work for a javascript (non jQuery) mecanisme?
    – Mahefa
    Commented Jun 24, 2020 at 15:55
16

(Latest as of 2022) For version Chrome Version Version 99:

Chrome Developer Tools - Event Listener

  1. Select the element you want to inspect

  2. Choose the Event Listeners tab

  3. Make sure to check the Framework listeners to show the real javascript file instead of the jquery function.

8

2018 Update - Might be helpful for future readers:

I am not sure when this was originally introduced in Chrome. But another (easy) way this can be done now in Chrome is via console commands.

For example: (in chrome console type)

getEventListeners($0)

Whereas $0 is the selected element in the DOM.

https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference#0_-_4

enter image description here

7

Edit: in lieu of my own answer, this one is quite excellent: How to debug JavaScript/jQuery event bindings with Firebug (or similar tool)

Google Chromes developer tools has a search function built into the scripts section

If you are unfamiliar with this tool: (just in case)

  • right click anywhere on a page (in chrome)
  • click 'Inspect Element'
  • click the 'Scripts' tab
  • Search bar in the top right

Doing a quick search for the #ID should take you to the binding function eventually.

Ex: searching for #foo would take you to

$('#foo').click(function(){ alert('bar'); })

enter image description here

3
  • 4
    Nice start, but what if I have 1500 references to #foo, most of them that are not binding anything, or in the case where I have multiple #foo IDs in external scripts that are not triggered in the present case ?
    – FMaz008
    Commented Sep 7, 2011 at 17:54
  • Great question. In my experience, that's where the human debugging process usually starts :) Commented Sep 7, 2011 at 18:05
  • 1
    Hehe, you're right, but my question was also about what I must do as a human :p
    – FMaz008
    Commented Sep 7, 2011 at 18:57
5

findEventHandlers is a jquery plugin, the raw code is here: https://raw.githubusercontent.com/ruidfigueiredo/findHandlersJS/master/findEventHandlers.js

Steps

  1. Paste the raw code directely into chrome's console(note:must have jquery loaded already)

  2. Use the following function call: findEventHandlers(eventType, selector);
    to find the corresponding's selector specified element's eventType handler.

Example:

findEventHandlers("click", "#clickThis");

Then if any, the available event handler will show bellow, you need to expand to find the handler, right click the function and select show function definition

See: https://blinkingcaret.wordpress.com/2014/01/17/quickly-finding-and-debugging-jquery-event-handlers/

4

For Chrome Version 52.0.2743.116:

  1. In Chrome's Developer Tools, bring up the 'Search' panel by hitting Ctrl+Shift+F.

  2. Type in the name of the element you're trying to find.

Results for binded elements should appear in the panel and state the file they're located in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.