Skip to content

JavaScript Events and Forms: addEventListener, input, submit and Validation

NeedMyTools Academy

Sign in free to read the full lesson

Sign in or create a free account

Free forever. No card, no paywall.

In the last lesson your code changed the page once, when it loaded. Real pages wait for people: someone clicks a button, types an amount or sends a form, and the page answers. Each of those actions is an event, and JavaScript lets you run a function whenever one happens. This lesson covers the few events you will use every day, how to read what a person typed, how to stop a form from reloading the page, and how to check the input and show a clear message when something is wrong. By the end you can build the core of any calculator on this site.

Quick answer

Attach a function to an element with el.addEventListener("click", handler). The browser calls it with an event object: event.target is the element that was used. Use the input event to react on every keystroke and change when a field is finished. Values from fields are strings, so convert them with Number(input.value) or input.valueAsNumber. For forms, listen for submit on the <form>, call event.preventDefault() to stop the page reload, check the values, and write errors or results with textContent. One listener on a parent can handle many children through bubbling and event.target.closest().

35 min read and practice · Content checked on 11 November 2026

In this lesson you will

  • Run code when people click, type or send a form, with addEventListener
  • Read values from inputs and convert them to numbers safely
  • Handle a form submit without a page reload and show clear error messages
  • Use event delegation so one listener handles a whole list

Listening for a click

addEventListener takes the name of an event and a function to run when it happens: button.addEventListener("click", addOne). Pass the function itself, without brackets. Writing addOne() would run it straight away, once, and hand its result to the browser instead. Most code uses an arrow function written in place, button.addEventListener("click", () => { ... }), when the handler is short and used only there.

The browser calls your function with one argument, the event object. Its most useful property is event.target, the element that was clicked or typed in. You may see onclick="..." written inside HTML in older pages. It works, but it mixes behaviour into the markup and allows only one handler per element, so keep HTML for structure and attach every listener from your script. Clicks also fire from the keyboard when a real <button> has focus and you press Enter or Space, which is one more reason to use buttons, not clickable <div> elements.

Try it: Quantity buttons that update a total
Code
ResultUpdates as you type
Console
  1. Console messages, errors, form data and clicked links appear here.

Press the buttons in the result. Each click runs its arrow function, which changes qty and redraws. The minus handler logs event.target.id, so you can see which element fired.

Sign in free to read the full lesson

You are reading the free preview. Create a free account (it takes one minute, no password) to unlock the rest of this lesson and track your progress.

Unlocks after sign-in:

  • 3 more sections with live code examples
  • Common mistakes to avoid
  • Hands-on coding exercise with automatic checks
  • Practice quiz and the graded lesson test
Sign in or create a free account

Free forever. No card, no paywall.

Questions people ask

What is the difference between the input and change events?

input fires every time the value changes, letter by letter, so it suits live results. change fires once the person has finished, when the field loses focus or a select, checkbox or radio button gets a new choice. Many calculators use input for text boxes and change for the rest.

Should I use onclick or addEventListener?

Use addEventListener. It keeps behaviour in your script, lets one element have several listeners, and supports options such as once. The onclick attribute still works and you will see it in older code, but each new assignment replaces the last one.

Is checking a form with JavaScript enough to keep bad data out?

No. Browser checks are there to help honest people fix mistakes quickly. Anyone can switch off JavaScript or send a request straight to your server, so the server must check every value again before saving or charging anything.

Why does my form reload the page when I press the button?

A <button> inside a form is a submit button by default, and submitting loads a new page. Either call event.preventDefault() in a submit listener, or, for a button that should not submit at all, give it type="button".

What does event.target mean?

It is the exact element where the event started, such as the button or the icon inside it that was clicked. event.currentTarget is the element whose listener is running, which differs from target when you use delegation on a parent.

Sources

Facts in this lesson were checked against these official pages on 11 November 2026.

Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.