Reputation: 762
I'm trying to track which element gets focus in a web app. I came across the monitorEvents API, but I'm having difficulty using it for control
or focus
events. Other events on body
are working as expected, but not the control
events. Any advice?
Upvotes: 1
Views: 4147
Reputation: 433
I was trying to monitor focus and blur events on the entire document today.
When using monitorEvents(document.body, 'control')
I would only see the event fired when I left the tab entirely and then refocused. I believe what's happening is that once you're focused on the body, it'll never change when you focus on child elements since the listener is at a high level and the events just bubble up to the body with no change in focus.
I tried monitoring events on a few specific child elements and saw that those triggered how I expected. So I decided to add an event listener to each element in the document and that worked well. Here's the one-liner for that.
document.querySelectorAll("*").forEach((el)=> monitorEvents(el, 'control'))
Upvotes: 1
Reputation: 25967
I'm not sure how exactly you want to "monitor" control
events, but you can set event listener breakpoints on the entire category, or individual events like focus
. Whenever a focus
listener runs for any node on the page, DevTools pauses on the first line of the listener.
Upvotes: 1