event.target

  • Revision slug: DOM/event.target
  • Revision title: event.target
  • Revision id: 78163
  • Created:
  • Creator: dbruant
  • Is current revision? No
  • Comment 68 words added

Revision Content

{{ DomRef() }}

Summary

This property of event objects is the object the event was dipatched on. It is different than event.currentTarget when the event handler is called in bubbling or capturing phase of the event.

Example

The event.target property can be used in order to implement event delegation.

// Assuming there is a 'list' variable containing an instance of an HTML ul element.
function hide(e){
  // Unless list items are separated by a margin, e.target should be different than e.currentTarget
  e.target.style.visibility = 'hidden';
}

list.addEventListener('click', hide, false);

// If some element (<li> element or a link within an <li> element for instance) is clicked, it will disappear.
// It only requires a single listener to do that

Browser compatibility

On IE6-8, the event model is different. Event listeners are attached with the non-standard element.attachEvent method. In this model, the event object is not passed as an argument to the event handler function but is the window.event object. This object has an srcElement property which has the same semantics than event.target.

Specification

DOM Level 2 Events: Event.target

{{ languages( { "pl": "pl/DOM/event.target" } ) }}

See also

Comparison of Event Targets

Revision Source

<p>{{ DomRef() }}</p>
<h2 name="Summary">Summary</h2>
<p>This property of event objects is the object the event was dipatched on. It is different than <code>event.currentTarget</code> when the event handler is called in bubbling or capturing phase of the event.</p> <h2 name="Example">Example</h2>
<p>The <code>event.target</code> property can be used in order to implement <strong>event delegation</strong>.</p>
<pre class="brush: js">// Assuming there is a 'list' variable containing an instance of an HTML ul element.
function hide(e){
  // Unless list items are separated by a margin, e.target should be different than e.currentTarget
  e.target.style.visibility = 'hidden';
}

list.addEventListener('click', hide, false);

// If some element (&lt;li&gt; element or a link within an &lt;li&gt; element for instance) is clicked, it will disappear.
// It only requires a single listener to do that
</pre><h2>Browser compatibility</h2>
<p>On IE6-8, the event model is different. Event listeners are attached with the non-standard <code>element.attachEvent</code> method. In this model, the event object is not passed as an argument to the event handler function but is the <code>window.event</code> object. This object has an <code>srcElement</code> property which has the same semantics than <code>event.target</code>.</p> <h2 name="Specification">Specification</h2>
<p><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event-target">DOM Level 2 Events: Event.target</a></p>
<p>{{ languages( { "pl": "pl/DOM/event.target" } ) }}</p><h2>See also</h2> <p><a href="/en/DOM/event/Comparison_of_Event_Targets" title="en/DOM/event/Comparison_of_Event_Targets">Comparison of Event Targets</a></p>
Revert to this revision