event.target property
The event.target
property contains
the element on which the event fired. This
is not the element to which the event
handler was attached, but the deepest
tag that was directly clicked on, for example.
Syntax
event.target;
Example
Let's say we have div
with a
paragraph inside it. Let's bind the
event to the div, but click on the
paragraph - in this case, event.target
will contain the end tag in which
the event happened - that is, the
paragraph, not the div. Verify
this with
tagName
:
<div id="div">
<p>text</p>
</div>
let div = document.querySelector('#div');
div.addEventListener('click', function(event) {
console.log(event.target); // displays a reference to a paragraph
});
See also
-
the
event.currentTarget
property
containing the element to which an event is bound -
the
code
property
that obtains the code of a pressed key -
the
event.key
property
that obtains an entered character