How to use Chome dev tool to find event handler bound to an element

Summary

Front-end developers frequently need to identify event handlers bound to HTML elements, a task often complicated by complex or obfuscated JavaScript code. Chrome DevTools offers a streamlined approach using the Sources tab, Event Listener Breakpoints, and Call Stack panel. To pinpoint an event handler, such as a click event, open DevTools, navigate to the Sources tab, and enable the desired event breakpoint under the Event Listener Breakpoints panel. Upon interacting with the target element on the webpage, the Call Stack will populate, allowing developers to step through the execution flow to precisely locate the bound event handler function. This technique significantly aids in debugging and understanding application behavior.

As a front end developer, there is frequent need to debug JS code and sometimes need to find out what event handler has been bound to a HTML element. In this post, we will show how to find out the click event handler bound to a HTML element, this same applies to other events as well.

Nowadays, a web application is usually very complicated and there are lots of JS codes which makes it difficult to find out what click event handler has been bound to a HTML element, especially when the JS source code has been obfuscated or compressed.

By using Chrome, it becomes relative easy to find out this kind of binding. Below three components of Chrome dev tool can be used.

  1. Sources tab
  2. Event Listener Breakpoints panel
  3. Call Stack panel

Take an example to demonstrate this.

1. Open a website and there is an element allowing user to click to post like, let's find out the click event handler bound to it.

2. Press F12 or right click on the element and select Inspect to open the Chrome dev tool and switch to Sources tab.

3. On the right, there is the panel Event Listener Breakpoints, find Mouse and expand it and tick click.

4. Go to Call Stack tab and it's empty now.

5. Click on the element on the web page, the Call Stack will be populated.

6. Click on the Step Into icon(the third one)(or press F11) above in highlighted area and keep doing that and you will find out the click event handler bound to it.

Happy coding and debugging!

JAVASCRIPT CHROME CHROME DEV TOOL EVENT LISTENER

  RELATED

  COMMENTS

0

No comment for this article.