aboutsummaryrefslogtreecommitdiff
path: root/files/id/web/api/event/target/index.html
blob: 52397c0827803b3303e65ed210a0becc5cd5eb6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
title: Event.target
slug: Web/API/Event/target
translation_of: Web/API/Event/target
---
<div>{{ApiRef("DOM")}}</div>

<p>The <code><strong>target</strong></code> property of the {{domxref("Event")}} interface is a reference to the object onto which the event was dispatched. It is different from {{domxref("Event.currentTarget")}} when the event handler is called during the bubbling or capturing phase of the event.</p>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox">var <em>theTarget</em> = <em>event</em>.target;</pre>

<h3 id="Value">Value</h3>

<p>{{domxref("EventTarget")}}</p>

<h2 id="Example">Example</h2>

<p>Properti <code>event.target</code> dapat digunakan untuk mengimplementasikan <strong>event delegation</strong>.</p>

<pre class="brush: js">// Membuat sebuah list
var ul = document.createElement('ul');
document.body.appendChild(ul);

var li1 = document.createElement('li');
var li2 = document.createElement('li');
ul.appendChild(li1);
ul.appendChild(li2);

function hide(e){
  // e.target refers to the clicked &lt;li&gt; element
  // This is different than e.currentTarget which would refer to the parent &lt;ul&gt; in this context
  e.target.style.visibility = 'hidden';
}

// Attach the listener to the list
// It will fire when each &lt;li&gt; is clicked
ul.addEventListener('click', hide, false);
</pre>

<h2 id="Spesifikasi">Spesifikasi</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th>Specification</th>
   <th>Status</th>
   <th>Comment</th>
  </tr>
  <tr>
   <td>{{SpecName("DOM WHATWG", "#dom-event-target", "Event.target")}}</td>
   <td>{{Spec2("DOM WHATWG")}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("DOM4", "#dom-event-target", "Event.target")}}</td>
   <td>{{Spec2("DOM4")}}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("DOM2 Events", "#Events-Event-target", "Event.target")}}</td>
   <td>{{Spec2("DOM2 Events")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("api.Event.target")}}</p>

<h3 id="Compatibility_notes">Compatibility notes</h3>

<p>On IE 6-8 the event model is different. Event listeners are attached with the non-standard {{domxref('EventTarget.attachEvent()')}} method. In this model, the event object has a {{domxref('Event.srcElement')}} property, instead of the <code>target</code> property, and it has the same semantics as <code>Event.target</code>.</p>

<pre class="brush: js">function hide(e) {
  // Support IE6-8
  var target = e.target || e.srcElement;
  target.style.visibility = 'hidden';
}
</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li><a href="/en-US/docs/Web/API/Event/Comparison_of_Event_Targets">Comparison of Event Targets</a></li>
</ul>