Using Chrome, how to find to which events are bound to an element

FMaz008 picture FMaz008 · Sep 7, 2011 · Viewed 106.6k times · Source

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.

Answer

Ionuț G. Stan picture Ionuț G. Stan · Sep 7, 2011

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