DOM Manipulation — JavaScript

JavaScript changes the page through the DOM, a version of the page in which every tag is an object. document.querySelector("selector") grabs the first element…

Since the first lesson, the results of your code have only appeared in the console, and visitors never see the console. In this lesson JavaScript starts to change the page itself: it replaces a heading, changes a colour, adds a new paragraph.

It does that through the DOM (Document Object Model). When the browser reads your HTML, it builds a version of the page that JavaScript can change. That version is called the DOM. In the DOM, every HTML tag becomes an object that you can grab, change, add or remove. As soon as the DOM changes, what you see on the page changes too.

Grab an element, then change it

The page has one heading:

<h1>Old Title</h1>

This JavaScript changes that heading:

const title = document.querySelector("h1");

title.textContent = "New Title";
title.style.color = "blue";

The result: the heading on the page now reads New Title, in blue.

If no element matches, querySelector returns null. The next line is then the same as writing null.textContent = ..., and you get the TypeError "Cannot set properties of null". When you see this error, check your selector again.

Property names must be exact

JavaScript treats uppercase and lowercase letters as different, so property names must be written exactly:

const button = document.querySelector("#btn");

button.onclick = () => {
  button.textContent = "Clicked";
};

When the button is clicked, its text changes. If you write onClick (with a capital C), the browser doesn't recognise it, so a click runs nothing.

Adding a new element

The page has one empty div:

<div id="box"></div>

This JavaScript makes a new paragraph, then puts it inside that div:

const p = document.createElement("p");
p.textContent = "Hello from JavaScript";
document.querySelector("#box").appendChild(p);

The result on the page: <div id="box"><p>Hello from JavaScript</p></div>. To remove an element, call p.remove().

textContent or innerHTML

These two properties treat HTML tags differently:

title.textContent = "<b>Hello</b>"; // shows exactly: <b>Hello</b>
title.innerHTML = "<b>Hello</b>";   // shows: Hello, in bold

textContent puts the text in as it is, so any HTML tags in it appear as plain text. innerHTML reads the text as HTML, so the tags in it really work. For plain text, especially text a user typed, use textContent. The reason: through innerHTML, harmful code hidden in the text can run too. This attack is called XSS.

⚠️ Why isn't the page changing?

TL;DR: JavaScript changes the page through the DOM, a version of the page in which every tag is an object. document.querySelector("selector") grabs the first element that matches the selector (written as in CSS), or null if there is none. el.textContent = "..." replaces the element's text. el.style.color = "blue" changes its style, with property names in camelCase (style.backgroundColor). To add an element: make it with document.createElement("p"), fill in its text, then put it on the page with parent.appendChild(p). Property names must be exact, for example onclick all in lowercase. Use textContent for plain text, and innerHTML only when the text really has to be read as HTML.

⚡ Grab an element, then change it

Write:

const title = document.querySelector("h1");

title.textContent = "New Title";
title.style.color = "blue";
Guess first: if the page has no <h1>, what does title hold?

null, because no element matches. The next line fails right away: the TypeError "Cannot set properties of null".

Meaning: document is the whole page. querySelector looks for the first element on the page that matches the selector, and you write selectors as in CSS. textContent replaces that element's text, and style.color changes its colour. What you see on the page changes right away.

⚡ Property names must be exact

Write:

button.onclick = () => {
  button.textContent = "Clicked";
};

Meaning: The function stored in onclick runs when its element (here button) is clicked. onclick is written all in lowercase. If you write onClick, the browser doesn't recognise it, so a click runs nothing. textContent is written with a capital C, and background-color from CSS becomes style.backgroundColor.

⚡ Make it, fill it, add it

Write:

const p = document.createElement("p");
p.textContent = "Hello from JavaScript";
document.querySelector("#box").appendChild(p);

Meaning: createElement makes a new element, here a paragraph, that you can't see on the page yet. textContent fills in its text. Only after appendChild does the paragraph go into the element #box and appear on the page.

What you will learn