addEventListener() lets you attach JavaScript code to DOM events (like clicks, key presses, focus, etc.) in a clean and flexible way. It:
onclick attributes)."click", "focus", "keydown", "mouseover".type, target, position, etc.{ once: true } for one-time listeners.removeEventListener() and a named function.General syntax of addEventListener():
element.addEventListener(eventType, handler, useCaptureOrOptions);
// eventType → string, e.g. "click", "keydown"
// handler → function to run when the event fires
// useCaptureOrOptions → optional:
// - Boolean (true = capture, false = bubble)
// - OR options object (e.g. { once: true, capture: true })
To remove a listener, the function must be the same reference:
function handleClick() {
console.log("Clicked!");
}
button.addEventListener("click", handleClick);
// Later:
button.removeEventListener("click", handleClick);
Attach a simple click event listener to a button:
<button id="myBtn">Click Me</button>
<script>
document.getElementById("myBtn").addEventListener("click", function() {
alert("Button was clicked!");
});
</script>
Named functions are reusable and easier to remove later. Anonymous functions are convenient for short, one-off handlers.
<button id="btnNamed">Greet</button>
<script>
function greetUser() {
console.log("Hello, user!");
}
document.getElementById("btnNamed").addEventListener("click", greetUser);
</script>
The handler receives an event object with details about what happened.
<button id="infoBtn">Show Info</button>
<script>
document.getElementById("infoBtn").addEventListener("click", function(e) {
console.log("Event type:", e.type);
console.log("Clicked element:", e.target.tagName);
});
</script>
To remove a listener, you must pass the same named function reference to removeEventListener().
<button id="add">Start</button>
<button id="remove">Stop</button>
<script>
function greet() {
alert("Hi there!");
}
const addBtn = document.getElementById("add");
const removeBtn = document.getElementById("remove");
addBtn.addEventListener("click", function() {
// Attach mouseover listener
addBtn.addEventListener("mouseover", greet);
});
removeBtn.addEventListener("click", function() {
// Remove mouseover listener
addBtn.removeEventListener("mouseover", greet);
});
</script>
You can attach multiple listeners to react to different events on the same element.
<input id="inputBox" placeholder="Hover or type">
<script>
const box = document.getElementById("inputBox");
box.addEventListener("focus", () => {
box.style.borderColor = "green";
});
box.addEventListener("blur", () => {
box.style.borderColor = "";
});
</script>
Using the options object, you can make a listener automatically remove itself after running once.
<button id="onceBtn">Click Once</button>
<script>
document.getElementById("onceBtn").addEventListener("click", () => {
alert("This will only happen once!");
}, { once: true });
</script>
By default, events are handled in the bubbling phase (from inner to outer elements). Passing true or { capture: true } switches to capturing (outer to inner).
<div id="outer" style="padding:20px;background:#ccc;">
<button id="inner">Inner</button>
</div>
<script>
document.getElementById("outer").addEventListener("click", () => {
console.log("Outer Div clicked - Capturing");
}, true);
document.getElementById("inner").addEventListener("click", () => {
console.log("Inner Button clicked - Bubbling");
});
</script>
The following mini demo uses addEventListener() to count how many times a div was clicked.
Click the box below and watch the counter update using a DOM event listener.
Total clicks: 0
Under the hood, JavaScript listens for theclick event on the box and updates the text content each time.{ once: true } for one-time events like welcome popups.e.stopPropagation() when you want to prevent bubbling to parent elements.DOMContentLoaded or at the bottom of the page).addEventListener().{ once: true } to restrict an alert to one-time only.addEventListener().event.stopPropagation().