skip to content

In the Chrome DevTools Console, how do you inspect a button's listeners and events, and why can't those helpers ship in a page script?

level: middleimportance: should knowfreq 30%

answer

  1. start from the selected element
  2. listeners registered versus events delivered
  3. empty result, so look at ancestors
  4. the utilities belong to the Console

basics

~10 s

Select the button in Elements, then use getEventListeners($0) for its registered listeners and monitorEvents($0, 'mouse') to log its events. These Console utilities work only from the Console, so a page script calling them fails.

solid answer

~50 s

I select the button in the Elements panel, which makes it `$0` in the Console. `getEventListeners($0)` returns an object with one array per event type, so I can see what is registered on that element, and passing a listener's function to `inspect()` opens it in Sources. If the array I expected is missing, the handler is probably delegated, so I try the same call on an ancestor or on `document`. `monitorEvents($0, 'mouse')` shows the other half, logging each event object as it arrives, and `unmonitorEvents` stops it. `$_` holds the last evaluated result, and `copy()` puts a string form of a value on the clipboard. None of this can ship: these helpers are the Console Utilities API, which works only when called from the Console and is not part of the web platform. A page script calling `copy` fails, and a page library defining `$` overrides the Console's `$`.

code

javascript · 5 lines
javascript
// Typed into the DevTools Console, with the button selected in the Elements panel.
getEventListeners($0);        // { click: [...], keydown: [...] }
getEventListeners(document);  // delegated handlers often sit on an ancestor
monitorEvents($0, 'click');   // logs each click event object
unmonitorEvents($0, 'click'); // stops it, so long as the selection has not moved

go deeper

for a junior

Recall that the Elements selection becomes $0 in the Console, and that helpers such as copy() and $_ exist only there. Pasting them into application code fails.

for a middle

Explain the difference between what is registered, from getEventListeners, and what is delivered, from monitorEvents. Know that the first looks only at the object you pass, so delegated handlers need an ancestor.

for a senior

Use these to find unknown code from a symptom with no redeploy: selection, listeners, ancestors, delivered events, then the function in Sources. Say how you separate two handlers from two events.

for a principal

There is little to decide here beyond habits. The useful angle is teaching a team these moves, since a Console diagnosis on production costs nothing and avoids adding logging that has to be shipped first.

## What these helpers are The **Console Utilities API** is a set of helpers that Chrome DevTools provides when you type into the Console: `$0`, `$_`, `$`, `$$`, `copy()`, `getEventListeners()`, `monitorEvents()`, `queryObjects()` and others. They are not part of JavaScript and not part of the web platform. The DevTools documentation is blunt about it: they only work when you call them from the Console, and they won't work if you call them in your scripts. That split explains the common failure. Code that worked in the Console gets pasted into the application, and at run time the name is simply undefined. It is not a timing issue or a matter of DevTools being open, because a page script is not where these names exist. Even inside DevTools, the availability is not universal: a **Watch** expression in Sources is evaluated without the command-line helpers, so `$0` typed there behaves differently from `$0` typed in the Console. The `$` helper has one extra wrinkle. If a page library defines its own `$`, that definition wins and the Console's `$` is overwritten, so `$('button')` may return a library object rather than an element. ## Finding what is registered A button behaves strangely, and you don't know which code handles it. Begin with the element: 1. Select the button in the **Elements** panel. The Console now exposes it as `$0`, and `$1` to `$4` hold earlier selections. 2. Run `getEventListeners($0)`. The result is an object with one array per registered event type, such as `click` or `keydown`, and each array has an entry for each listener. 3. Expand an entry to reach its function. Passing a function to `inspect()` opens it in the Sources panel. The result covers only the object you passed. That matters, because many applications attach a single listener high in the tree and let events bubble up to it, a pattern called **event delegation**. An empty result on the button is not proof that nothing handles it. Repeat the call on the parent, the application's root element, or `document`. ## Seeing what is delivered `getEventListeners` tells you what is registered. `monitorEvents` tells you what actually arrives, which is the other half of a diagnosis such as a click that seems to do nothing. - **One event:** `monitorEvents($0, 'click')` logs the Event object each time that event occurs on the object. - **Several events:** pass an array, as in `monitorEvents(window, ['resize', 'scroll'])`. - **A group:** pass `mouse`, `key`, `touch` or `control`, each mapped to a predefined set of events. - **Stopping:** `unmonitorEvents($0)` ends it. Note that `$0` follows your selection, so select something else in between and you are now addressing a different element. Hold the element in a variable if you plan to move around. The `mouse` group includes `mousemove`, so it floods the Console quickly. Prefer a single event once you know roughly what you are after. ## The smaller conveniences | Helper | What it gives you | Typical use | |---|---|---| | `$_` | The most recently evaluated value | Keep working on a result without retyping it | | `$$(selector)` | An array of matching elements | Count or map over elements from a query | | `copy(value)` | A string form of the value on the clipboard | Take a response object or node out to a file | | `queryObjects(Ctor)` | Objects created with that constructor | Check whether instances of a class exist | `queryObjects` works in the Console's currently selected execution context, so switching to an iframe's context changes what it can see. `$_` changes with every evaluation, including the one where you asked about it, so `$_.length` replaces the value it just read. ## A worked example A checkout button appears to fire twice. Select it and run `getEventListeners($0)`, and you find one `click` entry, so the button itself registers only one handler. Run the same call on `document` and find a second `click` handler there. Monitoring `click` on the button confirms that one physical click delivers one event, so the double action comes from two handlers rather than two clicks. You can now open both functions in Sources and read them. You added no logging, redeployed nothing and did not need a local build. ## What to avoid Don't leave these helpers in code, in any build, as a debugging aid. If a page really needs equivalent behaviour, use what the platform provides: `document.querySelector`, `addEventListener` and the Clipboard API. Note that the platform has no way to list the listeners on an element, which is why this particular helper is so useful, and why you can't simply reimplement it in a page script.

  • `getEventListeners($0)` shows no click entry, but clicking the button clearly runs code. What is going on?
    The call covers only the object you pass, so the handler is probably on an ancestor and reacts to the bubbled click. Try the parent, the application root or `document`. Monitoring click on the button also confirms the event is being delivered, which separates a delegated handler from a click that never arrives.
  • Why might `$('button')` in the Console return something other than an element?
    The Console's `$` is only a default. If the page loads a library that defines its own `$`, that definition overwrites the helper, so you get the library's result. `document.querySelector` is unaffected, and so is the Console's `$$`, unless the page defines that name too.
  • How do you check from the Console whether objects of a given class exist on the page?
    `queryObjects(Ctor)` returns an array of objects created with that constructor, such as `queryObjects(Promise)` or your own class. It works in the Console's currently selected execution context, so an iframe needs its own context selected. It answers whether instances exist, not why they are still alive.

  • The Console utilities work in page scripts as long as DevTools is open.
  • The $0 helper is a JavaScript global that any script on the page can read.
  • An empty getEventListeners result on an element proves nothing handles its events.
  • The monitorEvents helper lists the listeners registered on an object rather than logging events.
  • The Console's $ is always a query helper, whatever the page has defined.