Skip to content
UNWIND Code Lab Co-funded by the European Union
Changing the Page with JavaScript
Log In

Changing the Page with JavaScript

  • 4 min reading
  • 7 min activity
0 of 5 tasks doneup to 95 XP
  1. Quick recall10 XPOptional
  2. Find, change, see10 XPTo do
  3. textContent or innerHTML?15 XPTo do
  4. Change the heading30 XPTo do
  5. Add a skill30 XPTo do

Mission 4, step 2. Until now your JavaScript talked to the console. Today it changes the page itself.

In this lesson
  • 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.
Remember

In lesson 4.1 you used console.log(). Where did the messages appear?

Quick check

Quick recall

10 XP

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.

document html head body title h1#title p#box
Every tag of your HTML is a box in the DOM tree. JavaScript can find any box and change it.

Find, then change

document.getElementById("title") finds the element with id="title", or gives null. Keep the result in a constant.

Worked example
  1. Find
    // HTML: <h1 id="title">Hello world!</h1>
    const mainTitle = document.getElementById("title");
  2. Change the text
    mainTitle.textContent = "Changed with JavaScript!";
  3. Change the style
    mainTitle.style.color = "blue";

    In style, a CSS name loses its dash: background-color becomes backgroundColor.

Step by step

Find, change, see

Required 10 XP

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.

Grouping

textContent or innerHTML?

Required 15 XP
Practice

Change the heading

Required 30 XP

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.

Practice

Add a skill

Required 30 XP

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(), then textContent, innerHTML or style.
  • textContent for visitors' text, innerHTML only for your own HTML.
  • A new element: make it, fill it, add it with appendChild().
Next time

The kinds of values in JavaScript, and how to calculate with them.

Sources

  1. MDN Web Docs (2026). Introduction to the DOM.
  2. MDN Web Docs (2026). Document: getElementById() method.
  3. MDN Web Docs (2026). Node: textContent property and Element: innerHTML property (security considerations).
  4. MDN Web Docs (2026). HTMLElement: style property.
  5. 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

  • DOM
  • getElementById()
  • textContent
  • innerHTML
  • .style
  • createElement()
  • appendChild()
  • classList.add()