All subjects · Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

The Document Object Model (DOM): finding and changing page elements

Edexcel IAL Information Technology · WIT12 3 hour practical exam on web development with HTML, CSS and JavaScript

The DOM is the browser's model of the page as a tree of objects. JavaScript uses it to read and change content, styles and structure (spec 9.1.1).

Practise this in the app: instant marking and a fix for every mistake. Free, no email needed to start.

Practise this lesson

The DOM tree

When a browser loads HTML it builds the Document Object Model: a tree in which every element, attribute and piece of text is a node (an object). document is the root. html is its child, head and body are children of html, and so on. JavaScript can find any node, read or change its properties, and add or remove nodes, and the page on screen updates immediately without reloading.

<body>
  <h1 id="title">Classes</h1>
  <ul id="list">
    <li class="cls">Yoga</li>
    <li class="cls">Spin</li>
  </ul>
</body>

In this tree, body is the parent of h1 and ul; the two li elements are children of ul and siblings of each other.

Finding elements

MethodReturns
`document.getElementById("title")`The element with that id (or null)
`document.querySelector(".cls")`The first element matching a CSS selector
`document.querySelectorAll("li")`A list of every matching element (loop through it)
`document.getElementsByClassName("cls")`A live collection of elements with that class
`document.getElementsByTagName("li")`A live collection of elements with that tag

Changing elements

const t = document.getElementById("title");
t.textContent = "Our classes";                 // change the text
t.style.color = "navy";                        // change a style (camelCase)
t.style.backgroundColor = "#eef";
t.classList.add("highlight");                  // add a CSS class
t.classList.toggle("hidden");                  // add it if missing, remove it if present
document.getElementById("photo").src = "images/spin.png";
let typed = document.getElementById("email").value;   // read a form value
document.getElementById("list").innerHTML += "<li>Pilates</li>";
const li = document.createElement("li");       // build a new element
li.textContent = "Boxing";
document.getElementById("list").appendChild(li);

Worked example

A page has `<p id="score">0</p>` and a variable points = 12. Write JavaScript that shows 'Score: 12' in the paragraph in green.

  1. Find the element by id.
  2. Set its textContent to the joined string.
  3. Set style.color.

Answer: ```js let points = 12; const p = document.getElementById("score"); p.textContent = "Score: " + points; p.style.color = "green"; console.log(p.textContent + " / " + p.style.color); ``` Output: ``` Score: 12 / green ```

Key idea: The DOM is a tree of objects built from the HTML. Find elements with getElementById, querySelector and querySelectorAll; change textContent, innerHTML, value, src, style (camelCase) and classList; build elements with createElement and appendChild.

Check yourself

  1. What is displayed when this code runs? ```html <h2 id="head">Hello</h2> ``` ```js const h = document.getElementById("head"); h.textContent = "Welcome"; h.textContent = h.textContent + " back"; console.log(h.textContent); ```
    Show answer

    Welcome back. The text is replaced by Welcome and then ' back' is joined on: Welcome back.

  2. Which line sets an element's background colour with JavaScript?
    • el.style.backgroundColor = "yellow";
    • el.style.background-color = "yellow";
    • el.backgroundColor = "yellow";
    • el.style = background-color: yellow;
    Show answer

    el.style.backgroundColor = "yellow";. In JavaScript, hyphenated CSS properties are written in camelCase on the style object.

  3. What is displayed when this code runs? ```html <input id="qty" value="3"> <input id="price" value="2.5"> ``` ```js const q = document.getElementById("qty").value; const p = document.getElementById("price").value; console.log(Number(q) * Number(p)); ```
    Show answer

    7.5. The values are the strings "3" and "2.5"; converting and multiplying gives 7.5.

  4. Which method returns every element with class item, so a loop can change each one?
    • document.querySelector(".item")
    • document.querySelectorAll(".item")
    • document.getElementById("item")
    • document.querySelectorAll("item")
    Show answer

    document.querySelectorAll(".item"). querySelectorAll returns all matches; querySelector returns only the first. The selector needs the full stop for a class.

  5. What is the difference between textContent and innerHTML?
    • textContent can only be read, not changed
    • innerHTML only works on paragraphs
    • textContent changes the element's style
    • innerHTML treats tags in the string as HTML elements; textContent shows them as plain text
    Show answer

    innerHTML treats tags in the string as HTML elements; textContent shows them as plain text. innerHTML parses HTML; textContent does not, which is safer for user input.

  6. What does `menu.classList.toggle("open")` do?
    • Always adds the class open
    • Adds the class open if it is missing, or removes it if it is present
    • Deletes the menu
    • Opens a new window
    Show answer

    Adds the class open if it is missing, or removes it if it is present. toggle switches a class on and off, ideal for menus and modal boxes.

  7. What is the Document Object Model (DOM)?
    Show answer

    A tree-structured model of the web page in which each element is an object that JavaScript can access and change.. The DOM represents the page as a tree of objects (nodes) that scripts can read and change, updating the page without reloading it.

Practise with instant feedback

Sources

Last updated 8 November 2026. Spotted a mistake? Tell us.