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.
Console
- 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
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.
- MDN: Introduction to events
- MDN: EventTarget.addEventListener()
- MDN: Event bubbling
- MDN: Element input event
- MDN: HTMLFormElement submit event
- MDN: Event.preventDefault()
- MDN: FormData
- MDN: Client-side form validation
Code examples stay in English and run in a sandbox on your device. If you spot a mistake, tell us through the contact page.