JavaScript AcademyProfessional Engineering

Learning object 36 of 100

DOM tree inspector

Type HTML and see the tree of nodes the browser builds from it: elements with their attributes, text nodes, and how many children each element has.

Loading…

What it shows

The browser turns HTML into the DOM, a tree of objects. Every tag becomes an element node, and the text between tags, including line breaks and indentation, becomes text nodes. Properties such as children skip text nodes, while childNodes includes them. The browser also repairs invalid markup, so the tree can differ from what you typed. This inspector parses your HTML with DOMParser, which builds the tree without running any scripts.

Guided experiment

  1. Tick "Show whitespace-only text nodes" for the List sample. Where do the extra nodes come from?
  2. Open the Paragraph sample. How many child nodes does the p have, and how many element children?
  3. Load Broken markup and compare the tree with the HTML. What did the browser fix?

Predict

<p>Hi <b>there</b></p>: how many child nodes does the p have?

Related

Lessons: Lesson 51: Understanding the DOM tree

Projects: Project 9: Shopping Cart Price Calculator

Source: web/assets/js/objects/object-036.js in the companion repository.