JavaScript Event Listeners: Simple Examples

Summary

Pixel shares a beginner-friendly guide to JavaScript event listeners, explaining event targets, event types, handlers, and the addEventListener syntax. It demonstrates click, input, and keydown listeners, using the event object, removing listeners, and building a button counter—practical skills for creating interactive web pages.

DEV Community

JavaScript Event Listeners Explained with Simple Examples

Originally published on CodingCush. An event listener is JavaScript that waits for something to happen on a page, like a click, a key press or typing in a box, and then runs a function when it happens. You attach one with addEventListener: pick an element, name the event, and give the function to run. In this guide you will write click, input and keydown listeners, meet the event object, remove a listener, and build a button counter. Every result below matches a real test run of the same listener logic. What Is an Event? An event is a signal that something happened: a click, a key press, a typed letter, a submitted form. The browser notices each action and creates an event for it. Your job is to say what should happen in reply. Without listeners, a page just sits there looking polite. With listeners, it reacts: clicking Save can store a form, and typing in a search box can filter a list as you type. Three words matter. The target is the element the event happened on. The event type is what happened, like click or keydown. The handler is the function to run. The addEventListener Syntax, Step by Step The pattern has three parts: element.addEventListener("eventType", handlerFunction);
First, choose the element. Second, pass the event type as a string, in quotes. Third, pass the function to run. The MDN page for addEventListener lists the full syntax, including options you can ignore for now. To choose an element, you find it in the page structure, called the DOM. If that word is new, our guide to what the DOM is in JavaScript, explained simply shows how the page becomes a tree of elements you can select. One detail catches almost every beginner. Pass the function itself, not a call to it: showMessage, not showMessage(). The brackets would run it straight away, before any click. You are handing the browser a recipe, not serving the meal yet. Example 1: A Simple Click Listener Here is the smallest useful listener: Click me
const button = document.querySelector("#myButton");

button.addEventListener("click", function () {
console.log("Button clicked!");
});
Line by line: querySelector("#myButton") finds the button by its id. Then addEventListener says, when a click lands on this button, run this function, which prints one message to the console. Expected behaviour: nothing on page load. Click once and the console shows Button clicked! once. Click three times and it appears three times. The listener keeps listening. Example 2: A Button Counter That Remembers Now a pattern you will reuse everywhere: counting clicks. Count clicks

You clicked 0 times

let count = 0;
const counterButton = document.querySelector("#counterButton");
const output = document.querySelector("#countOutput");

counterButton.addEventListener("click", function () {
count = count + 1;
output.textContent = "You clicked " + count + " times";
console.log("You clicked " + count + " times");
});
The variable count lives outside the handler, so it survives between clicks. Each click adds one, updates the paragraph, and logs the same line. After three clicks, the paragraph and the final log line both read You clicked 3 times. The full log shows 1, then 2, then 3. The handler can still see count later because the function remembers the variables around it. That remembering has a name, and our guide to JavaScript closures explained with simple examples walks through it slowly. Terminal style code listing of the tested run: the click message, the counter reaching 3, input values H and Hi, the keydown results, and the removed handler at 1 call. Example 3: Input Events While Typing The input event fires every time the value of a text box changes, letter by letter:
const nameInput = document.querySelector("#nameInput");

nameInput.addEventListener("input", function (event) {
console.log("Current value: " + e...
JavaScript Event Listeners: Simple Examples
Powered by Readybot.io

The latest from Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …