Codectionary / Developer documentation / JavaScript

addEventListener() and the Event Object

addEventListener() attaches a function to run when a specific event occurs on an element, without overwriting any other listeners already attached (unlike the older onclick= style). The callback receives an Event object with details about what happened, including target (the element that triggered it) and methods like preventDefault().

Syntax

element.addEventListener("event", callback)

Examples

Basic Click Listener

Responding to a button click.

const button = document.querySelector("#myButton");

button.addEventListener("click", (event) => {
  console.log("Button clicked!");
  console.log("Target:", event.target);
});

preventDefault() and Form Submission

Stopping an event's default browser behavior.

const form = document.querySelector("#myForm");

form.addEventListener("submit", (event) => {
  event.preventDefault(); // stops the page from reloading
  console.log("Form submitted via JavaScript instead");
});

Multiple Listeners and removeEventListener()

Attaching several listeners and later removing one by reference.

function handleClick() {
  console.log("Clicked!");
}

const button = document.querySelector("#myButton");
button.addEventListener("click", handleClick);

// Later, stop listening - requires the SAME function reference
button.removeEventListener("click", handleClick);

// Anonymous functions cannot be removed this way, since a new reference is created each time

Best practices

  • Use addEventListener() instead of inline onclick= attributes or element.onclick = - it allows multiple listeners and cleaner separation of HTML and JavaScript
  • Keep a reference to named functions if you need to remove a listener later - removeEventListener() requires the exact same function reference
  • Call event.preventDefault() when you want to override default browser behavior, like a form submitting and reloading the page
  • Remove event listeners on elements that get removed from the DOM to avoid memory leaks in long-running applications

At a glance

Purpose
Application logic and interaction
File extension
.js ยท .mjs
Runs in
Browsers and JavaScript runtimes
Usually used with
HTML, CSS and Web APIs

Specifications & further reading

Related JavaScript documentation

Event Bubbling and Delegation
Most events "bubble" - after firing on the target element, they also fire on each ancestor element up to the document root, unless stopped with stopPropagation(). Event delegation takes advantage of bubbling by attaching a single listener to a parent element instead of many listeners on individual children, using event.target to identify which child was actually interacted with.
Arrow Functions
Arrow functions provide a more concise syntax for writing function expressions in JavaScript. Introduced in ES6, they use the => syntax and have some important differences from regular functions, particularly in how they handle the "this" keyword. Arrow functions are especially useful for callbacks, array methods, and short inline functions.
Async/Await
Async/await is modern JavaScript syntax for handling asynchronous operations, making asynchronous code look and behave like synchronous code. The async keyword marks a function as asynchronous, and await pauses execution until a Promise resolves. This approach makes asynchronous code more readable and easier to understand than traditional Promise chains or callbacks.
Variables: var, let, const
JavaScript has three ways to declare variables. var is the original way, function-scoped and hoisted with a default value of undefined. let, introduced in ES6, is block-scoped and can be reassigned. const is also block-scoped but cannot be reassigned after its initial value is set. Modern JavaScript strongly favors let and const over var.