Changing the Page with JavaScript
- 4 min reading
- 7 min activity
0 of 5 tasks doneup to 95 XP
Mission 4, step 2. Until now your JavaScript talked to the console. Today it changes the page itself.
- Find an element by its id with
getElementById(). - Change its text, its HTML and its style.
- Make a new element, give it a class and add it to the page.
In lesson 4.1 you used console.log(). Where did the messages appear?
Quick recall
The DOM
The browser turns your HTML into a tree of objects: the DOM (Document Object Model). document is the whole page. When JavaScript changes the tree, the page changes at once.
Find, then change
document.getElementById("title") finds the element with id="title", or gives null. Keep the result in a constant.
- Find
// HTML: <h1 id="title">Hello world!</h1> const mainTitle = document.getElementById("title"); - Change the text
mainTitle.textContent = "Changed with JavaScript!"; - Change the style
mainTitle.style.color = "blue";In
style, a CSS name loses its dash:background-colorbecomesbackgroundColor.
Find, change, see
textContent or innerHTML?
// HTML: <p id="message"></p>
const message = document.getElementById("message");
message.textContent = "<strong>Warning!</strong>"; // shows the tags as text
message.innerHTML = "<strong>Warning!</strong>"; // shows a bold word
textContent keeps tags as plain text, so it is safe for words that visitors type. innerHTML builds real elements. Use it only for HTML that you wrote yourself: visitors' text could hide harmful code. innerText gives the text as you see it. The slides say it gives tags too, but only innerHTML does.
textContent or innerHTML?
Change the heading
From slides 6 and 8 of the course: change the heading text and its colour, then put a bold word into the box.
Add new elements
createElement() makes an element in memory. You see it only after you add it with appendChild() or append() (at the end) or prepend() (at the start).
const newBox = document.createElement("div"); // make
newBox.textContent = "Hello, new box!"; // fill
newBox.classList.add("main-box"); // a class
newBox.id = "box-1"; // an id
newBox.style.padding = "20px"; // a style
document.body.appendChild(newBox); // add
On the slide, the last line is cut off at the edge. Without it, the box never appears.
Add a skill
Like slides 12 to 14 of the course: make a new list item in three steps and add it to the skills list.
- DOM
- The tree of objects that the browser builds from your HTML.
- textContent
- The text of an element. Tags in it stay plain text.
- innerHTML
- The HTML inside an element. Tags in it become elements.
- appendChild()
- Adds an element at the end of another element.
In short
- Find, then change:
getElementById(), thentextContent,innerHTMLorstyle. textContentfor visitors' text,innerHTMLonly for your own HTML.- A new element: make it, fill it, add it with
appendChild().
The kinds of values in JavaScript, and how to calculate with them.
Sources
- MDN Web Docs (2026). Introduction to the DOM.
- MDN Web Docs (2026). Document: getElementById() method.
- MDN Web Docs (2026). Node: textContent property and Element: innerHTML property (security considerations).
- MDN Web Docs (2026). HTMLElement: style property.
- MDN Web Docs (2026). Document: createElement() method, Node: appendChild() method and Element: classList property.
End of the lesson
This lesson: 0 of 105 XP
Code Cards in this lesson: 8
DOMgetElementById()textContentinnerHTML.stylecreateElement()appendChild()classList.add()