Builder14 min readBadge: Event Handler

JavaScript in the browser: events and the DOM

How JavaScript finds things on a page, reacts to clicks and typing, handles forms, and how frameworks like React do the same job for you.

The DOM: the page as a tree

When a browser loads HTML it builds the DOM (Document Object Model): a live tree of every element on the page. JavaScript can find elements in it, read them and change them, and the page updates straight away.

Events: run code when something happens

Clicks, key presses, typing and form submissions are events. You attach a function to an element with addEventListener, and it runs each time the event happens. For example: const button = document.querySelector('#like'); let likes = 0; button.addEventListener('click', () => { likes++; button.textContent = 'Likes: ' + likes }). Find the element, listen for the event, update the page.

Forms the right way

  • Listen for the form's submit event (it covers the Enter key too), not just a button click.
  • Call event.preventDefault() when you handle it yourself, so the page doesn't reload.
  • Input values are always text: convert numbers with Number() and trim spaces.
  • Check the input, show a clear message next to the field, and check again on the server, because browser checks can be bypassed.

Frameworks and debugging

React, Vue and similar frameworks handle the DOM for you: you change state, and they update the page. The ideas are the same: events trigger functions that change data, and the screen reflects the data. When something doesn't react, open the browser's developer tools, use console.log inside your handler to see whether it runs, and read the Console for errors.

Key terms

DOM (Document Object Model)
The browser's live tree of a page's elements, which JavaScript can read and change.
Event listener
A function that runs when something happens on the page, like a click or a form submit.
JavaScript
The programming language that makes web pages interactive.
Function
A named, reusable recipe in code that takes inputs and can return an output.
State
Data a component or app remembers and updates, like what's in a cart.
Browser console
The developer-tools panel (Inspect, then Console) that shows a page's errors and logs.
Developer tools (Inspect)
Browser tools that show a page's HTML and CSS and let you experiment live; changes aren't saved.
HTML
The language for a web page's structure: headings, paragraphs, buttons, images.
See the full glossary

Try it

Make a button do something

  1. Ask an AI for a single HTML file with a button and a counter, using addEventListener (no frameworks).
  2. Open it in your browser, click the button, and find the listener in the code.
  3. Add console.log inside the listener and watch the Console in developer tools.
  4. Add a small form with a number field that shows double the number on submit, using preventDefault and Number().

Ask an AI tutor about this guide

Copy a prompt into ChatGPT, Claude or Gemini to go deeper. Check what it tells you against the guide.

Study with an AI tutor: copy this whole guide with tutoring instructions, then paste it into your AI assistant.
  • Explain it like I'm 12
  • Quiz me (5 questions, one at a time)
  • Show me a real example for my situation
  • What do beginners get wrong?

Knowledge check

Five questions. Get 4 right to earn the Event Handler badge. You can retake it as often as you like.

Question 1 of 5

What is the DOM?

Finished the How software works guides? Take the How software works track test for ten mixed questions.

My notes

Write what clicked, what confused you, and what you want to try. Saved in this browser only.

Learn it live

Digital Builders University runs small live workshops where you build something real with an instructor beside you, starting with Vibe Coding 101.

See workshops

Practise it

Last reviewed November 25, 2026. AI tools change fast; check each tool’s own site for current features, plans and prices.