Event Listener
In one line
An event listener is a function that runs when a specific event (like a click or keypress) occurs on an HTML element.
In simple words
An event listener is how JavaScript responds to user actions. You attach a function to an element that runs when a specific event happens — a click, a key press, a form submission, a mouse move, and many more.
The standard method is addEventListener: element.addEventListener('click', handler). You can attach multiple listeners to the same element, and remove them with removeEventListener. The handler receives an event object with details about what happened.
Events "bubble" up the DOM tree by default — a click on a button also triggers click handlers on its parent, grandparent, and so on up to the document. This enables "event delegation" — attaching one listener to a parent that handles events from all its children.
Example
const button = document.querySelector("#myBtn");
button.addEventListener("click", (event) => {
console.log("Button clicked!", event.target);
});A click event listener that logs a message and the clicked element when the button is pressed.
Related terms
Related Resources
Related Lessons
- Event Handling
Learn event handling.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.