JavaScript Reference ยท DOM: Events
Event.target and Event.currentTarget
Reference for event.target, currentTarget, relatedTarget and composedPath(): find the element that triggered an event and the one whose listener is running.
Every event object tells you two different things about where it is. event.target is the element where the event originated, for example the exact button, icon or text container the user clicked. event.currentTarget is the element whose listener is running right now, which is the object you called addEventListener() on.
They are the same when you listen directly on the element that was clicked. They differ as soon as an event bubbles up from a descendant to a listener on an ancestor.
list.addEventListener("click", (event) => { console.log(event.target); // the element that was clicked console.log(event.currentTarget); // always `list` });
Try it
Syntax
event.target event.currentTarget event.relatedTarget // mouse, focus and pointer events only event.composedPath()
Properties and methods
| Name | Description |
|---|---|
target |
The object the event was dispatched to: the innermost element for a user action. Read-only. |
currentTarget |
The object whose listener is currently being called. It is null once the dispatch has finished. |
relatedTarget |
On mouseover/mouseout/mouseenter/mouseleave and focusin/focusout/focus/blur, the other element involved in the change, or null. |
composedPath() |
Returns an array of the objects the event will pass or has passed, from the target up to window. |
Usage notes
targetmay be a child you did not expect. Clicking an icon inside a button gives you the icon, not the button. Useevent.target.closest("button")to climb to the element you care about.- Arrow functions and
this. In an arrow function there is nothisbound to the element, so useevent.currentTarget. In a regular functionthis === event.currentTarget. currentTargetis reset. Reading it asynchronously, after anawaitor insidesetTimeout(), givesnull. Save it in a variable first.- Shadow DOM retargeting. When an event crosses a shadow boundary,
targetis retargeted to the shadow host for listeners outside.composedPath()shows the real path only for open shadow roots. thisvs delegation. In delegated listeners,currentTargetis the container andtargetidentifies the child. See event delegation.event.srcElement. A legacy alias oftarget. Do not use it.
Browser support
target, currentTarget and relatedTarget work in every browser. composedPath() is supported in all current browsers.
Related
- event delegation โ the main use of
target - event bubbling and capturing โ why they differ
Element.closest()โ climb fromtargetto the element you want- focus and blur events โ
relatedTargetfor focus - Lesson: Event delegation