Roland Gautier
Roland Gautier

Reputation: 345

How to get event.target and event.currentTarget in a webComponent (within a shadow root)

I tried to use the target object and the current target object of an event which occurs in one of my webComponents. It seems that these methods aren't reachable within a shadow root. Does anyone have a solution for this issue?

Upvotes: 1

Views: 852

Answers (1)

event.composedPath() returns an Array of all elements/Nodes the Event passed

so the target you are after is in: event.composedPath()[0]

unless there are mode:closed shadowRoots in between

https://developer.mozilla.org/en-US/docs/Web/API/Event/composedPath

The composedPath() method of the Event interface returns the event’s path which is an array of the objects on which listeners will be invoked. This does not include nodes in shadow trees if the shadow root was created with its ShadowRoot.mode closed.`

On Chrome/new Edge you can also use event.path

Upvotes: 1

Related Questions