typestar

Events in JavaScript

Listening for user interaction and form submission.

const form = document.querySelector("form");

form.addEventListener("submit", (event) => {
  event.preventDefault();
  const data = new FormData(form);
  console.log(Object.fromEntries(data));
});

document.addEventListener("keydown", (event) => {
  if (event.key === "Escape") {
    form.reset();
  }
}, { once: true });

How it works

  1. addEventListener binds a handler to an event.
  2. preventDefault stops the browser's default action.
  3. FormData reads the fields; { once: true } auto-removes.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
307
Tokens
87
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 52 seconds.

Type this snippet

Step 1 of 3 in Events, step 4 of 13 in The browser.

← Previous Next →