日本語

Monitoring DOM Events with monitorEvents in Chrome DevTools

​​Chrome DevTools​DOM,JavaScript

Type monitorEvents(要素, 'click') in the Chrome DevTools Console, and each time a click happens on that element, the Console prints one line with the event name and the event object. When a button does nothing, you can check whether click or key events are firing at all, and in what order, without adding console.log to the page's code. In a test environment, I watched a button, an input field, a form, and window, and checked what and how many lines appeared in the Console.

Note

The test environment was Ubuntu 24.04 with Chrome for Testing 153.0.8010.12 (the build bundled with Playwright). I launched Chrome with a window on a virtual display, opened DevTools on the right side of the window, and sent clicks, key presses, and mouse wheel input to the page. DevTools was in English. I did not test touch input ('touch'), zoom on window, elements inside an iframe, or Chrome on macOS or Windows.

monitorEvents() and unmonitorEvents() are documented in the Console Utilities API, together with $0 and copy(). They only work in the Console, and you cannot call them from the page's JavaScript.

The page I used for testing

The page has a name input and an "Add to cart" button inside a form. The button label is in Japanese and means "Add to cart". Pressing the button adds one to the cart count. Below them is an empty div 1500px tall, so the page can scroll.

html
<form id="order">
  <p><label>お名前 <input id="name" name="name"></label></p>
  <p><button type="button" id="buy">カートに入れる</button></p>
</form>
<p id="count">カート:0 点</p>
<div id="spacer"></div>
<script>
let n = 0;
document.getElementById('buy').addEventListener('click', () => {
  n++;
  document.getElementById('count').textContent = `カート:${n} 点`;
});
</script>

Watching the button's click

Type the following in the Console and press Enter. The return value is undefined, and nothing is printed at this point.

js
monitorEvents(document.querySelector('#buy'), 'click')

After this, I clicked the button three times, and the Console printed three click lines.

text
click  PointerEvent {isTrusted: true, pointerId: 1, width: 1, height: 1, pressure: 0, …}
click  PointerEvent {isTrusted: true, pointerId: 1, width: 1, height: 1, pressure: 0, …}
click  PointerEvent {isTrusted: true, pointerId: 1, width: 1, height: 1, pressure: 0, …}
Top: the page. Bottom: the Console. The page shows the cart count as 3 items (the label is in Japanese and reads "Cart: 3 items"). The Console shows the line monitorEvents(document.querySelector('#buy'), 'click'), then undefined, then three pairs of click and PointerEvent {isTrusted: true, …}. A VM7:1 link is on the far right.
The page and Console after clicking the button three times

The page showed the cart count as 3 items (the label is in Japanese), so the page's own click listener also ran as usual. In the Console, each line has the event name on the top row and the event object on the lower row, which has the ▶ triangle. (The code block above is a condensed copy of the text on screen, placed on one line per event.) Every line had a VM7:1 link on the right.

Expanding the event object shows the target

Click the ▶ on the left of a line to list the event object's properties. These are the main lines from the third click, after I expanded it:

text
isTrusted: true
clientX: 72
clientY: 156
currentTarget: null
detail: 1
eventPhase: 0
pointerType: "mouse"
target: button#buy
type: "click"

The element that was actually clicked appears as target: button#buy. currentTarget was null, and eventPhase was 0. As the MDN description of currentTarget explains, currentTarget has a value only inside an event handler, and it is null when you look at it from outside a handler.

The one-line summary (shown before you expand the ▶) can sometimes include a currentTarget value. When I tested the change event on the input field later, the summary showed target: input#name, currentTarget: input#name, eventPhase: 2.

Stopping the watch with unmonitorEvents

To stop watching, pass the same element to unmonitorEvents().

js
unmonitorEvents(document.querySelector('#buy'))

After this, clicking the button printed nothing in the Console, and the page showed 4 items in the cart (the label is in Japanese). The page's own listener was unaffected, and only the watch was removed.

Reloading the page also removed the watch. I watched the button's click with monitorEvents(), pressed F5 to reload, and then clicked the button. The Console printed nothing. (The reload reset the cart to 0, so this click made the count 1.) You have to retype monitorEvents() after every reload.

Watching related events together with 'mouse' or 'key'

The second argument can be an event type name, such as 'mouse', 'key', 'touch', or 'control', as well as an event name. The table in the monitorEvents section of the official documentation lists which events each type covers.

I watched the button with 'mouse'. I moved the mouse down from 50px above the button in 10px steps to the button's center, clicked, and then moved it 60px below the button. The Console printed eight lines in this order:

text
mouseover
mousemove
mousemove
mousedown
mouseup
click
mouseout
mouseleave

While the mouse keeps moving, mousemove keeps printing, and the output gets hard to read. As the official documentation also shows, you can pass an event name to unmonitorEvents() to remove only that event.

js
unmonitorEvents(document.querySelector('#buy'), 'mousemove')

When I repeated the same mouse movement, the two mousemove lines were gone, and I got six lines: mouseover, mousedown, mouseup, click, mouseout, and mouseleave.

Watching an input field selected in the Elements panel with $0

If you select the element you want to watch in the Elements panel, you can pass it with $0 without writing document.querySelector(). When I selected the input, the line showed == $0 on the right.

Elements panel. The form, p, and label nodes are expanded, the line <input id="name" name="name"> is selected, and == $0 appears on the right.
The input field selected in the Elements panel

Watching key input, focus, and value changes on an input field

With the input field still selected in the Elements panel, I ran monitorEvents($0, 'key') in the Console, clicked the input field, and typed a and then b. Each character produced four lines, for a total of eight.

Console. The result of $0 is <input id="name" name="name">. After monitorEvents($0, 'key'), there are two sets of keydown, keypress, textInput, and keyup, one set for a and one for b. The KeyboardEvent summaries show key: 'a', code: 'KeyA', and so on.
Console output when watching with 'key’ and typing a and b

keydown, keypress, and keyup are KeyboardEvents, and their summaries show the pressed key, for example key: 'a', code: 'KeyA'. textInput is a TextEvent, and it showed data: 'a' for the character entered.

I also watched the same input field with 'control'. I clicked the input field, typed c, and then clicked an empty part of the page. Four lines appeared: focus, input, change, and blur.

Type names and the events they actually watch

Some of the events I saw differed from the table in the official documentation. With 'mouse', I saw mouseleave, which is not in the table. (mouseenter did not appear, as described below.) With 'control', I saw input, which is also not in the table. Passing 'pointer', which is not in the table either, printed seven lines: pointerover, pointerenter, pointermove, pointerdown, pointerup, pointerout, and pointerleave.

The type names are converted to event names by the NormalizeEventTypes function in Blink's source in Chromium. The mapping written there matches the events I saw.

Type name Events watched (as written in the source)
'mouse' auxclick, click, dblclick, mousedown, mouseeenter, mouseleave, mousemove, mouseout, mouseover, mouseup, mousewheel
'key' keydown, keyup, keypress, textInput
'touch' touchstart, touchmove, touchend, touchcancel
'pointer' pointerover, pointerout, pointerenter, pointerleave, pointerdown, pointerup, pointermove, pointercancel, gotpointercapture, lostpointercapture
'control' resize, scroll, zoom, focus, blur, select, input, change, submit, reset

The source writes the 'mouse' entry as mouseeenter, with three e's, which is one more than the correct name. Because of this typo, mouseenter is not in the list. To watch mouseenter, pass its name directly, as in monitorEvents(要素, 'mouseenter'). Names that are not in the mapping are used as event names as they are.

Watching a parent form also shows events from its children

The watch works by adding a listener to the element you pass with addEventListener() (see SetMonitorEventsCallback in the source above). So if you watch a parent element, you also get events that happen on its children and bubble up to the parent.

I watched the form with click, focus, and focusin, and then clicked the button and the input field inside the form, in that order.

js
monitorEvents(document.querySelector('#order'), ['click', 'focus', 'focusin'])

The Console printed four lines: focusin, click, focusin, and click. The summary of the second focusin showed relatedTarget: button#buy, which tells you that focus moved from the button to the input field. No focus line appeared at all. As the MDN description of the focus event explains, focus does not bubble, so use focusin when you want to see focus on child elements from a parent.

With unmonitorEvents and no type, focusin is still watched

Next, I ran unmonitorEvents(document.querySelector('#order')) without a second argument, and then clicked the button. click stopped printing, but one focusin line still appeared.

Console. After the four lines from watching the form with click, focus, and focusin, there is one more focusin line after running unmonitorEvents(document.querySelector('#order')).
focusin still prints after running unmonitorEvents without a type

In the source above, when there is no second argument, the function substitutes a fixed list: the types 'mouse', 'key', 'touch', 'pointer', and 'control', plus events such as load, input, focus, and scroll. focusin is not in this list, so it was not removed. When I ran unmonitorEvents(document.querySelector('#order'), 'focusin'), clicking the button printed nothing. If you watched an event that is not in the list (such as focusin), pass the same name when you remove it.

Watching window's scroll and resize together with an array

If you pass an array of event names, you can watch several events at once. As in the official documentation's example, I watched resize and scroll on window.

js
monitorEvents(window, ['resize', 'scroll'])

I scrolled the page down three times with the mouse wheel. The page scrolled 360px (window.scrollY was 360), and the Console printed 35 scroll lines. The summary was scroll Event {isTrusted: true, type: 'scroll', target: document, currentTarget: Window, eventPhase: 3, …}. Three wheel turns alone produced 35 scroll lines.

I dragged the boundary between DevTools and the page 100px to the left, which changed the page width (innerWidth) from 724 to 624. This printed one resize line.

After I ran unmonitorEvents(window), nothing printed when I turned the mouse wheel. resize and scroll are in the default list used when there is no second argument, so both were removed together.

​Posted by 86