Skip to the page

The DOM and events · core

The DOM tree and building UI with plain DOM APIs

The DOM is a tree of node objects the browser builds from HTML; you query it (querySelector), create nodes (createElement, textContent), and insert them in batches (DocumentFragment, append) to avoid repeated layout.

The notes

What you must know

  • querySelector returns the first match or null; querySelectorAll returns a static NodeList; getElementsByClassName returns a live HTMLCollection.
  • Use textContent for text (safe); innerHTML parses HTML and is an XSS risk with user data.
  • Build many nodes in a DocumentFragment or one array and insert once with append / replaceChildren.
  • Element vs Node: children only has elements; childNodes includes text and comment nodes.
  • closest(selector) walks up to the nearest matching ancestor (useful with delegation).

The red pen

Common traps: mistake → fix

  • Mistake: "innerHTML is just faster"

    Fix: it re-parses, drops listeners on replaced nodes and runs injected markup.

  • Mistake: "querySelectorAll is live"

    Fix: it is a snapshot; getElementsBy* collections are live.

Easy to hard

Questions interviewers ask, easy to hard

  1. How would you render a list of 1,000 items with plain DOM APIs efficiently?
  2. Difference between textContent, innerText and innerHTML?
  3. Live vs static node lists?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview