Event Target Element
Predict Output Medium
Question
What does this code print when the button is clicked?
Code
<button id="btn">Click</button>
<script>
btn.addEventListener("click", (e) => {
console.log(e.target.tagName);
});
</script>
Hint
target is the element that triggered the event.
Explanation
event.target refers to the element on which the event was dispatched. For a click on the button, e.target is the button element, and e.target.tagName is "BUTTON" (tag names are uppercase in HTML). event.currentTarget would be the element the listener is attached to, which is the same here.
Related Resources
Related Lessons
- Event Handling
Learn event handling.
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.