JS DOM MASTER CLASS /// SELECT NODES /// MUTATE ELEMENTS /// BROWSER API /// JS DOM MASTER CLASS /// SELECT NODES /// MUTATE ELEMENTS /// BROWSER API ///

JavaScript DOM

Learn how JavaScript interacts with the browser. Select HTML elements, modify their content, and build truly dynamic interfaces.

dom-intro.js
1 / 13
12345
script.js
index.html (Reference)
🌳

Tutor:Welcome to the DOM! The Document Object Model is how JavaScript interacts with HTML. When a browser loads HTML, it converts it into a structured tree of 'Nodes'. Let's look at some basic HTML.


Skill Matrix

UNLOCK NODES BY LEARNING DOM API.

Concept: DOM Tree

The DOM represents an HTML document as a hierarchical tree. At the root sits the document object. Everything else (elements, text, comments) are nodes.

System Check

Which global object serves as the entry point to query the DOM tree?


Community Holo-Net

Showcase Your DOM Scripts

ACTIVE

Built an interactive widget or dynamic list? Share your DOM manipulation magic!

JavaScript DOM

Author

Pascual Vila

Frontend Instructor // Code Syllabus

The Document Object Model (DOM) is an essential concept. Without it, JavaScript couldn't change a webpage. The DOM bridges your JS code to the HTML document by representing the page as an Object Tree.

The Tree of Nodes

When a web browser loads an HTML file, it parses it and creates a model of it in memory. In this tree, the entire document is the root node. Every HTML tag is an element node, and the text inside them are text nodes.

JavaScript uses the global document object to query this tree.

Selecting Elements

Before you can change something, you have to point at it. We have multiple tools in our belt:

  • document.getElementById("my-id") - Fast and specific. Returns one element.
  • document.querySelector(".my-class") - Versatile. Uses CSS syntax. Returns the first match.
  • document.querySelectorAll("p") - Returns a NodeList of all matching elements.

Manipulating Elements

Once selected, the element behaves like a standard JavaScript object. You can access and mutate its properties:

const title = document.querySelector('h1');
title.textContent = 'New Title Here!';
title.style.color = 'blue';
title.classList.add('highlight');

Notice that you can change the text content, inject inline styles, or manipulate CSS classes using the classList API, which is often much cleaner than changing inline styles!

View Deep Dive on Node Creation+

To add elements dynamically, you use document.createElement("tagName"). This creates a "detached" node that exists in JS memory but not on the screen.

Once you set its attributes (like textContent or classes), you must attach it to an existing DOM node using methods like parent.appendChild(newNode) or parent.insertBefore(newNode, sibling).

DOM API Glossary

DOM

Document Object Model. An API that represents and interacts with any HTML or XML document as a tree structure.

api.js

document

The root object of the HTML document. It serves as an entry point into the web page's content.

api.js

getElementById

A method that returns an Element object representing the element whose id property matches the specified string.

api.js

querySelector

Returns the first Element within the document that matches the specified CSS selector.

api.js

textContent

Gets or sets the text content of a node and its descendants. Safe from XSS injection.

api.js

createElement

Creates the HTML element specified by tagName. It sits in memory until appended.

api.js