How to Add an Event Listener in JavaScript
Use addEventListener() when JavaScript should react to clicks, typing, form submissions, scrolling, or any browser event. This is the cleanest way to connect user actions with dynamic behavior.
What you’ll build or solve
You’ll learn how to add an event listener in JavaScript using the standard DOM method. You’ll also know how to pass callback functions and target different event types.
Learn JavaScript on Mimo
When this approach works best
This approach is the right choice when UI behavior depends on user interaction or browser state.
Common real-world scenarios include:
- Button clicks
- Form submission
- Input typing
- Keyboard shortcuts
- Scroll reactions
Prefer this over hardcoding behavior in HTML attributes like onclick; event listeners keep logic cleaner and easier to scale.
Prerequisites
You only need:
- A basic HTML page
- A JavaScript file or
<script>tag - Basic DOM selection knowledge
Step-by-step instructions
Step 1: Select the element and attach the listener
Start by selecting the target element.
HTML
<button class="cta">Start</button>
<script>
const button = document.querySelector(".cta");
button.addEventListener("click", () => {
console.log("Button clicked");
});
</script>
The first argument is the event type, and the second is the callback function.
You can listen for many event types, not just clicks.
JavaScript
const input = document.querySelector(".email");
input.addEventListener("input", () => {
console.log("Typing...");
});
What to look for:
- Select the element first
- The first argument is the event name
- The second argument is the callback
- Great for reusable interaction logic
- Cleaner than inline HTML event attributes
Examples you can copy
Button click
JavaScript
const button = document.querySelector(
".cta"
);
button.addEventListener("click", () => {
alert("Started");
});
Form submit
JavaScript
const form = document.querySelector(
"form"
);
form.addEventListener("submit", (event) => {
event.preventDefault();
});
Input typing
JavaScript
const search = document.querySelector(
".search"
);
search.addEventListener("input", () => {
console.log("Searching...");
});
Common mistakes and how to fix them
Mistake 1: Calling the callback immediately
What the reader might do:
JavaScript
button.addEventListener("click", handleClick());
Why it breaks: the function runs immediately instead of waiting for the event.
Corrected approach:
JavaScript
button.addEventListener("click", handleClick);
Mistake 2: Forgetting to select the element
What the reader might do:
JavaScript
button.addEventListener("click", () => {});
Why it breaks: if button was never declared, JavaScript throws ReferenceError: button is not defined.
Corrected approach:
JavaScript
const button = document.querySelector(".cta");
Mistake 3: Using the wrong event type
What the reader might do:
JavaScript
input.addEventListener("click", () => {});
Why it breaks: text fields usually need input or change.
Corrected approach:
JavaScript
input.addEventListener("input", () => {});
Troubleshooting
If you see TypeError: Cannot read properties of null (reading 'addEventListener'), the selector did not match or the script ran before the HTML existed. Fix the selector, move the script below the HTML, or wait for DOMContentLoaded.
If the event never fires, confirm the selector matches the element you’re using and the event name is spelled correctly.
If the callback runs immediately, remove the parentheses.
If typing events do not work, switch from click to input.
Quick recap
- Use
addEventListener()for interactions - Select the element first
- Pass the event name and callback
- Do not call the callback immediately
- Great for scalable UI behavior
Join 40M+ people learning for free on Mimo
4.8 out of 5 across 1M+ reviews
Check us out on Apple AppStore, Google Play Store, and Trustpilot