The DOM and events

ECMAScript 2026

The browser turns HTML into a tree in memory called the Document Object Model, or DOM. Browser APIs are operations the browser provides for JavaScript. They can find and change elements or respond to a click.

On this page

Find an element with querySelector

document represents the page. querySelector accepts a CSS selector and returns its first match or null when no match exists. textContent replaces the text of the element that was found.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Find an element with querySelector</title>
</head>
<body>
  <p id="message">Preparing your order…</p>
  <script>
    const message = document.querySelector("#message");
    message.textContent = "Your order is ready.";
  </script>
</body>
</html>

What to expect The page shows the updated sentence “Your order is ready.”.

If the result differs If nothing changes and the Console mentions null, check that the selector matches the ID and that the script runs after the paragraph has been read.

Create a list item and append it

createElement("li") builds an element in memory. Set its text, then call append on the parent to insert it into the page. Use textContent for a plain text value.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Create a list item and append it</title>
</head>
<body>
  <ul id="catalog"><li>Pen</li></ul>
  <script>
    const item = document.createElement("li");
    item.textContent = "Notebook";
    document.querySelector("#catalog").append(item);
  </script>
</body>
</html>

What to expect The list shows Pen and then Notebook.

Update and remove existing list items

After selecting a list item, assign textContent to change its label. remove() detaches an element from its parent. The browser redraws the page using the updated tree.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Update and remove existing list items</title>
</head>
<body>
  <ul><li id="pen">Pen</li><li id="folder">Folder</li></ul>
  <script>
    document.querySelector("#pen").textContent = "Blue pen";
    document.querySelector("#folder").remove();
  </script>
</body>
</html>

What to expect Only Blue pen remains in the list.

Toggle a class on an element

classList is the list of classes on an element. toggle() adds the class if it is missing and removes it if it is present. For operations based on an ID, confirm that the element exists and belongs to the list before changing it.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Toggle a class on an element</title>
</head>
<body>
  <ul id="catalog"><li id="pen">Pen</li></ul>
  <script>
    const catalog = document.querySelector("#catalog");
    const item = document.getElementById("pen");
    if (item && item.matches("li") && catalog.contains(item)) {
      item.classList.toggle("featured");
    }
  </script>
</body>
</html>

What to expect Inspect the Pen item to see class="featured". Try looking up a missing ID and confirm the page still loads without an error.

Listen for a button click

An event is a notification from the browser that something happened. addEventListener takes the event name and a function to run when it occurs. That function is called an event listener.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Listen for a button click</title>
</head>
<body>
  <button id="ready" type="button">Mark ready</button>
  <p id="status">Waiting</p>
  <script>
    const button = document.querySelector("#ready");
    const status = document.querySelector("#status");
    button.addEventListener("click", () => {
      status.textContent = "Your order is ready.";
    });
  </script>
</body>
</html>

What to expect The page starts with Waiting. Click the button and the message changes to “Your order is ready.”.

Follow a click as it bubbles

Many events, including click, bubble from the element where they start to ancestors. event.target is the element the event started on in this example. event.currentTarget is the element whose listener is currently running.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Follow a click as it bubbles</title>
</head>
<body>
  <div id="catalog"><button type="button" id="pen">Pen</button></div>
  <p id="result"></p>
  <script>
    document.querySelector("#catalog").addEventListener("click", event => {
      document.querySelector("#result").textContent =
        event.target.id + " → " + event.currentTarget.id;
    });
  </script>
</body>
</html>

What to expect Click Pen. The page shows pen → catalog because the catalog listener received the button’s click.

Use one listener for a group of buttons

Event delegation means putting the listener on a shared parent. closest() looks for the nearest element matching the selector, starting with the click target. dataset reads data-* attributes. Ignore clicks that did not start within one of the intended buttons.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Use one listener for a group of buttons</title>
</head>
<body>
  <div id="catalog">
    <button type="button" data-product="Pen"><strong>Pen</strong></button>
    <button type="button" data-product="Notebook">Notebook</button>
  </div>
  <p id="result"></p>
  <script>
    const catalog = document.querySelector("#catalog");
    catalog.addEventListener("click", event => {
      const button = event.target.closest("button[data-product]");
      if (!button || !catalog.contains(button)) return;
      document.querySelector("#result").textContent = button.dataset.product;
    });
  </script>
</body>
</html>

What to expect Click either button, including the bold text inside Pen. The result displays the product name.

Read form values on submit

The form receives a submit event after the browser checks required controls. preventDefault() keeps the browser on this page. FormData can collect values by the name attribute.

How to try it Save this example as a new .html file, then open it in your browser. After a change, save and reload.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Read form values on submit</title>
</head>
<body>
  <form id="order">
    <label for="product">Product</label>
    <input id="product" name="product" required>
    <button type="submit">Confirm</button>
  </form>
  <p id="result"></p>
  <script>
    const order = document.querySelector("#order");
    order.addEventListener("submit", event => {
      event.preventDefault();
      const values = new FormData(order);
      document.querySelector("#result").textContent = values.get("product");
    });
  </script>
</body>
</html>

What to expect Submit an empty field to see browser validation. Type Pen and submit again. Pen appears below the form.

Official references