JavaScript Events: Responding to User Interaction

Events are how your code reacts to what users do — clicks, typing, form submissions. Learn addEventListener, the event object, common event types, and build a fully interactive feature from scratch.

Published July 22, 20266 min readBy ACY Partner Indonesia
JavaScript events — addEventListener and user interaction
300 × 250Ad Space AvailablePlace your ad here

We’ve reached the final piece, and it’s the one that ties everything together. You can already store data, write logic, and change the page with the DOM. But so far your code runs once when the page loads, then stops. Events are what let your code wait for the user to do something — click a button, type in a field, submit a form — and react to it. This is the heartbeat of every interactive website.

Back in the very first JavaScript article, we said JavaScript is fundamentally about “an event, and code that reacts to it.” This is where that idea becomes real. Let’s make pages truly interactive.

What an event is

An event is something that happens on the page, usually because of the user. A click is an event. Typing a key is an event. Submitting a form, moving the mouse, the page finishing loading — all events. The browser is constantly firing off events as the user interacts with the page.

Your job is to listen for the events you care about and run code when they fire. The function you hand over is called an event handler — it “handles” the event by running in response to it. The whole model boils down to one sentence: when this event happens on this element, run this function.

addEventListener: the main tool

The standard way to listen for an event is the addEventListener method. You call it on an element, telling it which event to listen for and what function to run:

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

button.addEventListener("click", () => {
  console.log("The button was clicked!");
});

Read it out loud: “on the button, listen for a click, and when it happens, run this function.” Now every time the user clicks that button, the function runs and the message prints. Click it five times, the function runs five times. The page is finally responding to the user.

The structure is always the same: element.addEventListener("eventName", handlerFunction). The first argument is the event name as a string ("click", "input", etc.), and the second is the function to run.

addEventListener is the recommended way to handle events

You might remember the inline onclick="..." attribute from the first JavaScript article. It works, but addEventListener is the better choice for real code: it keeps your JavaScript out of your HTML (the same clean separation we’ve valued all along), it lets you attach multiple handlers to the same element, and it hands you more control. Make addEventListener your default way to respond to events.

Common events

"click" is the one you’ll use most, but there are many event types for different interactions. A few you’ll meet often:

Event Fires when…
click An element is clicked
input The value of an input changes (as you type)
submit A form is submitted
mouseover / mouseout The mouse enters / leaves an element
keydown A key is pressed
load The page (or an image) finishes loading

Each one lets you respond to a different kind of interaction. The "input" event, for instance, is how live search works — you listen for input on a search box and update the results as the user types:

const searchBox = document.querySelector("#search");

searchBox.addEventListener("input", () => {
  console.log("Current text:", searchBox.value);
});

Notice searchBox.value — that’s how you read the current text out of an input field. Every keystroke fires the input event, so you watch the text update live.

The event object

Your handler function can take one argument: the event object, which carries information about what happened. By convention it’s called event (or just e):

button.addEventListener("click", (event) => {
  console.log(event.target);   // the element that was clicked
});

The event object carries useful properties — event.target is the element the event happened on, and for keyboard events event.key tells you which key was pressed. You won’t always need it, but it’s there the moment you do.

One especially common use is event.preventDefault(), which stops the browser’s built-in behavior. The classic case is a form: by default, submitting one reloads the page. To handle the submission with JavaScript instead, you cancel that default first:

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

form.addEventListener("submit", (event) => {
  event.preventDefault();   // stop the page from reloading
  console.log("Form handled by JavaScript!");
});

preventDefault is essential for forms and links

event.preventDefault() is one of the most useful things to keep in your back pocket. Without it, submitting a form reloads the page and clicking a link navigates away — usually not what you want once you’re handling things in JavaScript. Calling preventDefault() at the top of your handler tells the browser “skip your usual behavior; I’ve got this.” You’ll reach for it constantly with form submissions and link clicks.

Putting it all together: a complete feature

Let’s build something real that pulls in everything from this whole section — DOM selection, an event, a conditional, and updating the page. Here’s a simple to-do adder: type a task, click a button, and watch it appear in a list.

The HTML:

<input id="task-input" type="text" />
<button id="add-btn">Add Task</button>
<ul id="task-list"></ul>

The JavaScript:

const input = document.querySelector("#task-input");
const button = document.querySelector("#add-btn");
const list = document.querySelector("#task-list");

button.addEventListener("click", () => {
  const text = input.value;

  if (text === "") {
    return;   // do nothing if the input is empty
  }

  const li = document.createElement("li");
  li.textContent = text;
  list.appendChild(li);

  input.value = "";   // clear the input for the next task
});

Take a moment to appreciate what this does. When the button is clicked, the code reads the input’s text, checks that it isn’t empty (a conditional), creates a fresh <li> element (the DOM), fills it with the text, adds it to the list, and clears the input. The user types a task, clicks, and watches it land in the list — a genuinely interactive feature, built entirely from the pieces you’ve learned. This is real web development.

Wrapping up — and the end of the frontend journey

Events bring your pages to life, and now you can build interactivity:

  • An event is something that happens on the page (a click, a keystroke, a form submit); your code listens for it and reacts.
  • addEventListener("event", handler) is the standard way to respond to events — cleaner and more flexible than inline onclick.
  • Common events include click, input, submit, and mouse/keyboard events; read input values with .value.
  • The event object (event.target, event.key) carries details, and event.preventDefault() stops default browser behavior — essential for forms and links.
  • Combining events, the DOM, conditionals, and the rest gives you complete, interactive features.

And with that, you’ve reached the end of the frontend fundamentals. Look back at how far you’ve come: from writing your first HTML tag, to styling pages with CSS, to programming with JavaScript and making pages respond to users. Those three layers — structure, style, and behavior — are the foundation of everything on the web, and you now understand all three.

The best thing you can do from here is build. Take these pieces and make something small of your own — a to-do list, a tip calculator, a little interactive page. Every real project teaches you more than any article ever could, and you already have everything you need to start.

Tags:javascriptfrontendeventsinteractivitybeginner
728 × 90Ad Space AvailablePlace your ad here

Related Articles

See All Articles

You Might Also Like

Frontend best practices overview cover
Frontend / Fundamentals

Frontend Best Practices: An Overview

A friendly map of what good frontend really means — semantic HTML, clean CSS, unobtrusive JavaScript, responsive layouts, performance, and progressive enhancement, with pointers to go deeper.

Sep 20, 20268 min read
A Frontend Developer Roadmap cover with the path HTML to CSS to JavaScript
Frontend / Fundamentals

A Frontend Developer Roadmap: What to Learn, and in What Order

A calm, step-by-step learning path for beginners: HTML, CSS, JavaScript, developer tools, responsive and accessible design, a framework, TypeScript, and deployment, with the reasoning behind each step.

Sep 20, 202611 min read
Title card reading Styling and Interactivity over a dark blue ACY Partner background
Frontend / Fundamentals

How Styling and Interactivity Work

A beginner-friendly look at how CSS turns plain HTML into a designed layout, and how JavaScript adds behavior — the structure, style, behavior mental model for building web pages.

Sep 20, 20269 min read
Three front-end layers combining on one web page
Frontend / Fundamentals

How HTML, CSS, and JavaScript Work Together

A hands-on walkthrough for beginners: build one small button, give it structure with HTML, looks with CSS, and behavior with JavaScript, and watch the three layers cooperate on a real page.

Sep 20, 20268 min read
The Frontend Developer Toolkit cover with editor, browser, and CLI motifs
Frontend / Fundamentals

The Frontend Developer Toolkit

A friendly tour of the everyday tools a frontend developer relies on — code editor, browser and DevTools, terminal, version control, package managers, and a dev server — and what each one is actually for.

Sep 20, 202610 min read
Cover illustration for What Frontend Developers Do
Frontend / Fundamentals

What Frontend Developers Do: A Beginner's Guide to the Role

A clear, beginner-friendly look at what frontend developers actually do every day: turning designs into working interfaces, building reusable UI, and caring about responsiveness, accessibility, and speed.

Sep 20, 202610 min read