The currentTarget
read-only property of the Event
interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to Event.target
, which identifies the element on which the event occurred and which may be its descendant.
Syntax
var currentEventTarget = event.currentTarget;
Value
Examples
Event.currentTarget
is interesting to use when attaching the same event handler to several elements.
function hide(e){
e.currentTarget.style.visibility = 'hidden';
console.log(e.currentTarget);
// 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++){
// Console: print the clicked <p> element
ps[i].addEventListener('click', hide, false);
}
// Console: print <body>
document.body.addEventListener('click', hide, false);
// Click around and make paragraphs disappear
Note: The value of event.currentTarget
is only available while the event is being handled. If you console.log()
the event
object, storing it in a variable, and then look for the currentTarget
key in the console, its value will be null
. Instead, you can either directly console.log(event.currentTarget)
to be able to view it in the console or use the debugger
statement, which will pause the execution of your code thus showing you the value of event.currentTarget
.
Specifications
Specification | Status | Comment |
DOMThe definition of 'Event.currentTarget' in that specification. | Living Standard | |
DOM4The definition of 'Event.currentTarget' in that specification. | Obsolete | |
Document Object Model (DOM) Level 3 Events SpecificationThe definition of 'current event target' in that specification. | Obsolete | |
Document Object Model (DOM) Level 2 Events SpecificationThe definition of 'Event.currentTarget' in that specification. | Obsolete | Initial definition |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
currentTarget
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 1 |
IE Full support 9 Full support 9 No support 6 — 9 Notes' On Internet Explorer 6 through 8, the event model is different. Event listeners are attached with the non-standard |
Opera
Full support 7 |
Safari
Full support 10 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 10 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
- See implementation notes.'
- See implementation notes.
See also
Event.currentTarget by Mozilla Contributors is licensed under CC-BY-SA 2.5.