designwithfocus

JavaScript & front end

DOM vs HTML: The File and the Live Document

By Design With Focus Team 7 min read
A source file on the left and a live document tree on the right

Short answer

HTML is the markup you ship. The DOM is the live tree the browser builds in memory so scripts can read and change the document. MDN says parsing HTML builds that tree and then the browser uses it to display the page. The Living Standard's parser outputs a Document. An attribute in the file is a string. The matching DOM property may be a number or a boolean.

Last verified against the sources listed below.

Key takeaways

  • HTML is the file. The DOM is the in-memory tree built from that file.
  • The DOM is a Web API, not a part of the JavaScript language.
  • Scripts change the live tree. They do not by themselves edit the saved file.
  • getAttribute reads a string. The matching property can use another type.
  • A boolean attribute is true when present, even if the value says false.

DOM vs HTML is the difference between a file of markup and the live tree a browser builds from it. You edit HTML to change what is deployed. You use the DOM to change what is on the page after that file loads.

MDN’s DOM introduction says the DOM connects a page to scripts by representing the document in memory. It is a logical tree. Each branch ends in a node, and nodes hold objects.

Methods on those objects change structure, style, or content. Nodes can also have event handlers. The DOM is not a programming language, and it is not part of JavaScript.

It is a Web API the language calls.

The HTML parser is how the file becomes that tree. The input is a stream of code points. Tokenization, then tree construction, produces a Document.

MDN says that when a browser parses an HTML document, it builds a DOM tree and then uses it to display the document.

HTML DOM
What it is Markup in a file or response The live tree in memory
Who changes it Whoever edits and deploys the file Scripts in the page, and the parser while it builds
A typical read View source document.querySelectorAll and element properties
An attribute Text in the start tag A string via getAttribute, or a property that may convert the type
After a script runs The saved file is unchanged The tree can contain nodes the file never had

How does the file become the tree?

The browser parses the HTML and builds the tree. The file is the input. The Document is the output.

MDN’s picture of a small page is html, then head and body, with a title, an h1, and a p. It warns that the real DOM keeps whitespace, so a diagram that only draws elements is incomplete. The document object stands for the document.

Element interfaces such as HTMLTableElement add HTML-specific features on top of the core DOM. The HTML DOM API is that HTML layer. SVG has its own API on the same idea.

You do not set up the DOM before you use it. A script in the page calls it. MDN’s example is document.querySelectorAll("p"), which must return the p elements.

paragraphs[0] is the first. That list is the live tree, not a copy of the source text.

What the file is for, before script touches it, is covered in the HTML guide.

Events travel on this tree after it exists. The event bubbling guide explains that path. The introduction here only needs MDN’s line that nodes can carry handlers and that those handlers run when the event fires.

HTML attribute vs DOM property?

The attribute is the string in the markup. The property is the JavaScript value on the node, and it may not be the same string.

MDN’s attribute reference names the two faces. The content attribute is what you set in the HTML. getAttribute and setAttribute use it.

It is always a string, even when the meaning is a number. To set maxlength to 42 as a content attribute, MDN says you call setAttribute("maxlength", "42").

The IDL attribute is the property, element.foo. MDN says it reflects the content attribute: reading it can transform the underlying value, and writing it stores something back into the content attribute. Properties are not always strings.

input.maxLength is a number. If you pass another type, JavaScript’s ordinary conversion applies.

MDN’s type example shows a real disagreement. The default type of an input is text. If you set input.type to foobar, the content attribute keeps foobar, but the IDL attribute returns text, because that is the type the control actually uses.

Most properties follow the specification. MDN says a few, such as select.size, behave oddly, and you should read the spec for those.

Boolean attributes are a sharper case. Presence means true. Absence means false.

MDN says true and false are not allowed as the way to mean those states. checked="false" is true, because the attribute is present. Valid markup uses an empty value or the attribute’s name.

To leave a box unchecked, do not write the attribute.

The style attribute is the same split in another form. MDN’s HTMLElement.style says the property reflects the inline style attribute. You cannot replace the style object, but assigning element.style.color sets one inline declaration.

Assigning element.style itself replaces the whole inline style, like setting cssText. A length in that inline style uses CSS units. A CSS pixel is 1/96 of a CSS inch, which is a styling fact, not a change to the HTML file.

Which one should you change?

Change the HTML file when the next page load should include the edit. Change the DOM when the current page should update without a new file.

Use the file for the document you deploy: headings, the controls that exist on first paint, and attributes that should be in view source. Use the DOM when a click, a fetch, or a render should add, remove, or restyle nodes. MDN says that is the point of the model.

Google’s robots meta guide shows why the difference matters outside your own script. It says Google typically renders pages to index them, and rendering is not guaranteed, so some extraction can happen before or after that render. If you add DOM elements with JavaScript and you need data-nosnippet on them, include the attribute when you insert the node.

Adding it later on a node that already exists is what the page tells you to avoid. The file and the live tree can be read at different moments.

A component that emits JavaScript updates the live document when it runs. An SVG to JSX converter produces that JavaScript from an SVG, rather than leaving you with only a static file. The converter does not replace the DOM.

It gives you code that runs against it.

What stays easy to mix up?

View source shows the HTML that was delivered. The Elements panel shows the DOM after parsing and after script. They match only until something mutates the tree.

Whitespace is the quiet mismatch. MDN says the tree preserves it. A formatter that strips blank lines from a diagram is not showing every text node.

Types are the loud mismatch. A string from getAttribute("maxlength") and a number from the property can both be “right.” Compare them only after you know which face you read. For booleans, test the property or test whether the attribute exists.

Do not test whether the attribute’s string is the word false.

Style is a third mismatch. A rule in a stylesheet is not the style attribute. element.style sees inline styles.

The cascade can still paint a color that element.style.color does not list. That is CSS plus the DOM, not a second copy of the HTML file.

Keep the file and the tree as two objects. Ship corrections in HTML when they should survive a reload. Use the DOM when the page has to react.

Read attributes as strings and properties as the typed view MDN describes, and expect those views to differ on purpose.

Frequently asked questions

Is the DOM the same thing as the HTML file?

No. MDN says a web page can be shown in the window or read as HTML source, and that both are the same document, while the DOM is the representation scripts can change. The tree follows the markup, and MDN notes that the real tree also preserves whitespace, so a tidy diagram can leave text nodes out. Saving the HTML file on a server does not run your script. The script runs in the browser, on the tree that was built after the file was fetched.

Does JavaScript include the DOM?

Not as part of the language. MDN says the DOM is a Web API. JavaScript in a browser uses it to reach the document. The same language in Node.js gets a different set of APIs, and the DOM is not a core part of that runtime. MDN also shows a short Python example using a DOM parser, to make the point that the model is not tied to one language. In the browser, you call the API from a script. You do not install the DOM first.

Why does getAttribute disagree with the property?

They are two faces of most attributes. MDN calls the content attribute the value in the HTML, always a string, read with getAttribute and set with setAttribute. The IDL attribute is the JavaScript property. It reflects the content attribute and can transform it. MDN's maxlength example stores the string 42 through setAttribute, while the maxlength property is a number. MDN's type example is sharper: setting the property to foobar leaves foobar in the content attribute, but the property reads back as text, the default type.

What does checked equals false do?

It checks the box. MDN says boolean attributes are true when present and false when absent. The strings true and false are not the switch. If the attribute is present, its value must be empty or a case-insensitive match for the attribute name, with no extra whitespace. checked equals false is still present, so it counts as true. Omit the attribute when the control should start unchecked. The property you read in script is the boolean. The attribute is the markup.

Can script add elements the file never had?

Yes. That is the live tree. Google's robots meta guide says the live document can differ from the original HTML when script adds nodes, and it tells you to put data-nosnippet on those nodes at the moment you add them if you need the attribute. MDN says DOM methods can change structure, style, and content, and that nodes can carry event handlers. The file on disk stays as you deployed it until you edit that file. View source and the Elements panel differ after script runs.

Sources