event.currentTarget

  • Revision slug: DOM/event.currentTarget
  • Revision title: event.currentTarget
  • Revision id: 18965
  • Created:
  • Creator: ziyunfei
  • Is current revision? No
  • Comment 46 words added, 30 words removed

Revision Content

{{ DomRef() }}

Summary

Identifies the current target for the event, as the event traverses the DOM. It always refers to the element the event handler has been attached to as opposed to event.target which identifies the element on which the event occurred.

Example

event.currentTarget is interesting to use when attaching the same event handler to several elements.

function hide(e){
  e.currentTarget.style.visibility = "hidden";
  // When this function is used as an event handler: this === e.currentTarget
}

var ps = document.getElementsByTagName('p');

for(var i = 0; i < ps.length; i++){
  ps[i].addEventListener('click', hide, false);
}

// click around and make paragraphs disappear

Browser compatibility

On Internet Explorer 6 through 8, the event model is different. Event listeners are attached with the non-standard element.attachEvent method. In this model, there is no equivalent to event.currentTarget and this is the global object. One solution to emulate the event.currentTarget feature is to wrap your handler in a function calling the handler using Function.prototype.call with the element as a first argument. This way, this will be the expected value.

Specification

DOM Level 2 Events: Event.currentTarget

See also

Comparison of Event Targets

{{ languages( { "pl": "pl/DOM/event.currentTarget" ,"zh-cn": "zh-cn/DOM/event.currentTarget" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Identifies the current target for the event, as the event traverses the DOM. It always refers to the element the event handler has been attached to as opposed to <code>event.target</code> which identifies the element on which the event occurred.</p>
<h2 id="Example" name="Example">Example</h2>
<p><code>event.currentTarget</code> is interesting to use when attaching the same event handler to several elements.</p>
<pre class="brush: js">function hide(e){
  e.currentTarget.style.visibility = "hidden";
  // When this function is used as an event handler: this === e.currentTarget
}

var ps = document.getElementsByTagName('p');

for(var i = 0; i &lt; ps.length; i++){
  ps[i].addEventListener('click', hide, false);
}

// click around and make paragraphs disappear
</pre>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>On Internet Explorer 6 through 8, the event model is different. Event listeners are attached with the non-standard <code>element.attachEvent</code> method. In this model, there is no equivalent to <code>event.currentTarget</code> and <code>this</code> is the global object. One solution to emulate the <code>event.currentTarget</code> feature is to wrap your handler in a function calling the handler using <code>Function.prototype.call</code> with the element as a first argument. This way, <code>this</code> will be the expected value.</p>
<h2 id="Specification" name="Specification">Specification</h2>
<p><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-Event-currentTarget">DOM Level 2 Events: Event.currentTarget</a></p>
<h2 id="See_also">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>
<p>{{ languages( { "pl": "pl/DOM/event.currentTarget" ,"zh-cn": "zh-cn/DOM/event.currentTarget" } ) }}</p>
Revert to this revision