beginner24 min

Events and Forms

Respond to clicks and form submissions, and read what a user typed.

What you'll learn

  • Attach event listeners with addEventListener
  • Read values from form inputs
  • Prevent a form's default submission behavior when handling it in JavaScript

Prerequisites

Explanation

An event is something that happens in the browser: a click, a key press, a form submission, a page finishing loading. You react to events with addEventListener, which takes an event name and a function to run when it fires:

const button = document.querySelector("#save");
button.addEventListener("click", () => {
  console.log("Button was clicked!");
});

The function you pass is called an event handler. It automatically receives an event object as its argument, which carries details about what happened — including, for forms, a way to stop the browser's default behavior.

Forms fire a submit event when the user presses Enter in a field or clicks a submit button. By default, submitting a form reloads the page — almost never what you want in an interactive app. Call event.preventDefault() at the start of your handler to stop that:

const form = document.querySelector("form");
form.addEventListener("submit", (event) => {
  event.preventDefault();
  const nameInput = document.querySelector("#name");
  console.log("You typed:", nameInput.value);
});

Every text input, textarea, and select element exposes its current content through its .value property — that's how you read what a user has typed at the moment your handler runs. For a checkbox, you'd read .checked instead of .value.

A common beginner pattern is validating input before acting on it — e.g. checking that a text field isn't empty before adding it to a list — which combines everything from this lesson: an event listener, reading .value, and a conditional.

Example

Handling a form submission, reading an input's value, and preventing the page reload.

<!doctype html>
<html>
  <body>
    <form id="greet-form">
      <input id="name-input" type="text" />
      <button type="submit">Greet</button>
    </form>
    <p id="output"></p>
    <script>
      document.querySelector("#greet-form").addEventListener("submit", (event) => {
        event.preventDefault();
        const name = document.querySelector("#name-input").value;
        document.querySelector("#output").textContent = "Hello, " + name + "!";
      });
    </script>
  </body>
</html>

Try it yourself

Change the greeting text or add a check for an empty name.

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Guided exercise

Guided exercise

Add a click handler to the button with id 'increment' that increases the number shown in the element with id 'count' by 1 each time it's clicked.

Checks: Clicking the button three times shows 3

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Independent exercise

Independent exercise

Handle the form's submit event: prevent the default reload, read the #task-input value, and if it is not empty, append it as a new <li> to #task-list.

Checks: Adds a non-empty task as a list item · plus 1 hidden check

Code editor. Press Escape then Tab to leave the editor if keyboard focus becomes trapped. Press Control+Shift+M inside the editor to toggle Tab-key focus trapping.

Loading editor…

Stuck? Get a hint.

Common mistakes

  • Forgetting event.preventDefault(), causing the page to reload and lose all JavaScript state.
  • Reading .textContent instead of .value when trying to get what a user typed into an input.
  • Attaching the event listener before the element exists in the DOM, so it silently does nothing.

Knowledge check

Knowledge check

1. What does event.preventDefault() do when called inside a form's submit handler?
2. How do you read the current text a user typed into an <input>?
3. What is the first argument to addEventListener?

Takeaway

addEventListener plus preventDefault turns a static form into logic you fully control.

Summary

addEventListener attaches a handler function to react to events like click and submit. preventDefault stops a form's default page reload, and .value reads the current content of an input so your handler can act on it.

References

Your notes

Notes save automatically.

Finished this lesson?

Mark it complete to track your progress and schedule a future review.