mc0de
JavaScript · Lesson 11 of 17

The JavaScript DOM

Bring your webpages to life! Discover how JavaScript finds HTML elements, changes content, updates styles, and creates entirely new parts of a webpage.

DOM Tree Selectors textContent Dynamic Elements
● ● ●   dom.js
const title =
  document.getElementById(
    "pageTitle"
  );

title.textContent =
  "Hello, DOM!";
LIVE WEBPAGE PREVIEW

My Webpage

JavaScript can change this page!

01 / Getting Started

What Is the Document Object Model?

HTML provides the structure of a webpage, CSS controls its appearance, and JavaScript makes it interactive. But how does JavaScript know which heading, paragraph, or button to change?

The answer is the Document Object Model (DOM). When a browser loads an HTML document, it creates an object-based representation of that document. JavaScript can use this representation to read and modify the page.

A simple HTML document
<body>
  <h1>Welcome!</h1>
  <p>Let's learn JavaScript.</p>
  <button>Click Me</button>
</body>

The browser represents these elements as nodes in a tree. The heading, paragraph, and button become elements that JavaScript can access.

A simplified DOM tree
document └── html └── body ├── h1 ├── p └── button
Think of it this way: HTML builds the webpage, and the DOM gives JavaScript a way to work with that webpage after it has loaded.
02 / Selecting Elements

Find the HTML You Want to Change

Before changing an element, JavaScript needs to locate it. You can find elements by their ID or use CSS-style selectors.

HTML
<h1 id="pageTitle">Welcome!</h1>
<p class="message">Hello there.</p>
JavaScript selectors
const title = document.getElementById("pageTitle");

const message = document.querySelector(".message");

console.log(title.textContent);
console.log(message.textContent);
Console output
Welcome! Hello there.

getElementById() finds an element using its ID. querySelector() finds the first element matching a CSS selector, such as .message for a class or #pageTitle for an ID.

If nothing matches, these methods return null. Always ensure the element exists before trying to modify it.

03 / Changing Page Content

Update Text With JavaScript

One of the simplest DOM operations is changing what an element displays. The textContent property lets you read or replace an element’s text.

Changing a heading
<h1 id="greeting">Hello!</h1>

<script>
  const heading = document.getElementById("greeting");
  heading.textContent = "Welcome to mC0de!";
</script>
Result on the webpage
Welcome to mC0de!

JavaScript changes the heading without requiring the visitor to reload the entire page.

You may also encounter innerHTML, which interprets strings as HTML markup. It’s useful for trusted markup, but should not be used to insert untrusted user input.

Best practice: Use textContent when displaying ordinary text. It treats the value as text rather than HTML.
04 / Changing Styles

Control How Elements Look

JavaScript can also change an element’s appearance. The style property provides access to inline CSS styles.

Updating styles
const title = document.getElementById("pageTitle");

title.style.color = "teal";
title.style.fontSize = "32px";
title.style.textAlign = "center";

Notice that CSS property names containing hyphens are written in camelCase when accessed through JavaScript. For example, font-size becomes fontSize.

For larger style changes, it’s often cleaner to add or remove CSS classes instead.

Using classList
const card = document.querySelector(".card");

card.classList.add("highlighted");
card.classList.remove("hidden");
card.classList.toggle("active");

classList.add() adds a class, remove() removes one, and toggle() switches it on or off.

05 / Creating New Elements

Add HTML Elements Dynamically

Sometimes a webpage needs to display something that wasn’t present when it first loaded. JavaScript can create new elements and insert them into the document.

Create and append a paragraph
const container = document.getElementById("content");

const paragraph = document.createElement("p");
paragraph.textContent = "This paragraph was created by JavaScript!";

container.appendChild(paragraph);

createElement() creates an element in memory. appendChild() inserts it at the end of the selected container.

To add multiple elements, you can repeat these steps in a loop—especially useful when rendering an array of items.

Connection to Lesson 08: Arrays and DOM manipulation work together beautifully. Loop through an array, create a list item for each value, and append it to a webpage.
06 / Removing & Updating Elements

Keep Your Page in Sync

JavaScript can remove elements that are no longer needed. The remove() method removes an element from its parent.

Removing an element
const notice = document.getElementById("notice");

if (notice) {
  notice.remove();
}

It’s helpful to check that an element exists before calling methods on it.

You can also replace the contents of a container and rebuild it from your current data.

Rebuilding a list
const items = ["HTML", "CSS", "JavaScript"];
const list = document.getElementById("courseList");

list.replaceChildren();

for (const course of items) {
  const item = document.createElement("li");
  item.textContent = course;
  list.appendChild(item);
}

replaceChildren() clears the container when called without arguments. The loop then creates a fresh list from the array.

07 / The DOM in Action

Build a Live Page Editor

Let’s combine element selection, text updates, and styling into a small demonstration. Use the controls below to customize a preview card.

Live DOM Explorer

Welcome to My Page

This content can be changed using JavaScript.

Ready! Try customizing the preview.

Every button selects or creates DOM elements and updates the webpage. No full-page refresh is necessary.

Coming next: You’ll explore events in greater detail in Lesson 12. For now, notice how a click can trigger JavaScript that changes the DOM.
🚀 mC0de Challenge

Build a Webpage Customization Studio

Create a small website editor that lets visitors personalize a preview card using JavaScript.

Your Mission

  1. Create a preview card containing a heading and paragraph.
  2. Add a text input for a custom heading.
  3. Use getElementById() to select the heading.
  4. Use textContent to display the new heading.
  5. Add a button that changes the card’s background color.
  6. Create a button that adds a new paragraph using createElement() and appendChild().
  7. Add a Reset button that restores the original content and styles.
Extra challenge: Add a Remove Last Paragraph button and a theme selector. Use classList instead of changing every style individually.
💻 Interactive JavaScript Sandbox

Build Your Webpage Customization Studio!

The starter lets you edit the heading, change the background, add paragraphs, and reset the preview. Select Load Solution for theme classes, individual paragraph removal, and a more polished interface.

Live Result
Lesson 11 Recap

What You’ve Learned

You’re Controlling Real Webpages!

You now know how JavaScript interacts with HTML through the DOM, turning static documents into dynamic experiences.

  • Understand the DOM as a tree of document nodes.
  • Select elements using getElementById() and querySelector().
  • Change text using textContent.
  • Update styles using style and classList.
  • Create and append elements dynamically.
  • Remove elements and rebuild content.
  • Build an interactive Webpage Customization Studio.

Next up: Events — learn how to respond to clicks, typing, keyboard input, and other user interactions!

Your Learning Journey

0 / 17 Completed

You’re 11 lessons into the JavaScript pathway. Keep building!

🎉

You've Mastered the DOM Basics!

You can now select HTML elements, change their content and appearance, create new elements, and remove them dynamically. Your JavaScript skills are becoming much more practical!