The DOM

Knowledge Videos

Short animated explainers, organised by subject.

The DOM

The DOM is the tree of nodes the browser builds from your HTML. Find a node with document.querySelector and change it with textContent.

Try it

Transcript

When a browser opens your H T M L, it builds a tree of objects in memory. This tree is called the D O M, the Document Object Model. Every element is a node in the tree.

JavaScript can reach the page through the document object. The query selector method finds one element, using the same selectors as C S S.

Once you have the element, you can change it. Set its text content, and the page changes at once. The H T M L file stays the same. Only the tree in the browser changes.

To run your script, add a script tag at the end of the body, so the elements already exist. Then open the console to try things out.

Remember: the D O M is the tree the browser builds from your page. Use document dot query selector to find a node, and text content to change it.

More in this series