Event.target
The target
property of the Event
interface is a reference to the object onto which the event was dispatched. It is different from Event.currentTarget
when the event handler is called during the bubbling or capturing phase of the event.
Syntax
const theTarget = someEvent.target;
Value
EventTarget
Example
The event.target
property can be used in order to implement event delegation.
// Make a list const ul = document.createElement('ul'); document.body.appendChild(ul); const li1 = document.createElement('li'); const li2 = document.createElement('li'); ul.appendChild(li1); ul.appendChild(li2); function hide(evt) { // e.target refers to the clicked <li> element // This is different than e.currentTarget, which would refer to the parent <ul> in this context evt.target.style.visibility = 'hidden'; } // Attach the listener to the list // It will fire when each <li> is clicked ul.addEventListener('click', hide, false);
Specifications
Specification |
---|
DOM Standard (DOM) # ref-for-dom-event-target③ |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
target |
1 |
12 |
1 |
9 |
7 |
1 |
1 |
18 |
4 |
10.1 |
1 |
1.0 |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Event/target