How do I view events fired on an element in Chrome DevTools?
To swiftly grasp events on an element in Chrome, you'll need to access the DevTools, select the Sources tab, then head to the Event Listener Breakpoints located to your right. You can expand categories like Mouse or Keyboard, and pick specific events to observe such as click
or keypress
. When you trigger the event on your website, the page pauses and lets you perform real-time debugging.
Turn on the click
breakpoint from the Sources tab, click on your button, and Chrome DevTools will bring the event center stage for you to inspect.
Console utilities for event diagnoses
Take your event investigation a notch higher by using the monitorEvents()
function in the Console tab of DevTools. With this utility, you have Sherlock-level observation on all the events an element emits.
To end your espionage, simply type:
You can make your monitoring more specific by indicating an event type:
Event types dissected
Deep diving into event types can significantly boost your ability to debug efficiently:
Ways around monitoring custom events
Working with custom events or a library like jQuery may require an approach beyond regular DevTools event monitoring. Custom events, particularly those sparked by scripts, might remain elusive.
Here's how you can approach this:
- Check the script for event names.
- Implement
live()
oron()
handy jQuery methods and set breakpoints in the handlers. - Visual Event, a bookmarklet utility, is handy for detecting handlers linked to DOM elements.
- For jQuery-related events, use the console command
$._data($0, 'events')
to expose the associated events and handlers.
Picking apart event handlers
If you're looking for the script that manages the event, follow the event handler's pathway back to its origin:
- Right-click the function under the Event Listeners section.
- Opt for 'Go to function definition'.
- DevTools exposes the source code.
Diving deep into form events
When it comes to events attached with form elements, things can get complicated with validation activities or interaction with external libraries:
- Analyze the methods by which frameworks link events to form controls.
- Identify hidden event listeners with the help of utility functions.
- Libraries could wrap or delegate events making it necessary to manually debug or seek guidance from community forums and related documentation.
Was this article helpful?