← The DOM and the browser console

Find and change

Find an element on a page with JavaScript, change its text and colour, and read what the console says.

Every button that opens a menu, every counter that goes up and every live search is JavaScript changing a page that is already on screen.

A web page is a tree of elements. JavaScript can find one and change it while the page is open. Two steps do most of the work:

  1. Find it: document.querySelector("#title") finds the element with id="title". A # means an id, a . means a class (".note"), and a plain word means a tag ("h1").
  2. Change it: title.textContent = "New words" replaces its text, and title.style.color = "crimson" changes its colour.

console.log(...) writes a message in the Console under the page, to see what your code is doing.

The hard part is the selector. It is just text, so a typo finds nothing: the answer is null, and the next line fails with "Cannot set properties of null". When you see null, check the selector first.

PredictRead the code and say what it will do before you run anything. A wrong guess is fine: it shows you what to look for.

Predict

page.html
<h1 id="title">Sports day</h1>
<p class="note">Bring a hat.</p>
<script>
  const title = document.querySelector("#title");
  title.textContent = "Sports day: moved to Friday";
  console.log(title.textContent);
</script>

Does the script run before or after the heading has appeared?

What does the heading say once the page has loaded?

RunRun it and compare the result with your prediction.

Run

Press Run. The page starts as plain HTML, and then the script changes two elements. The Console shows what console.log wrote.

page.html
<h1 id="title">Sports day</h1>
<p class="note">Bring a hat.</p>
<script>
  const title = document.querySelector("#title");
  title.textContent = "Sports day: moved to Friday";
  const note = document.querySelector(".note");
  note.textContent = "Bring a hat and water.";
  note.style.fontStyle = "italic";
  console.log(title.textContent);
</script>
Try it yourselfYour edits are saved in this browser.

InvestigateTrace how each line of code connects to what you see.

Investigate

page.html
<h1 id="title">Sports day</h1>
<p class="note">Bring a hat.</p>
<script>
  const title = document.querySelector("#title");
  title.textContent = "Sports day: moved to Friday";
  const note = document.querySelector(".note");
  note.textContent = "Bring a hat and water.";
  note.style.fontStyle = "italic";
  console.log(title.textContent);
</script>

Match code with meaning.

The browser reads from the top.

The error shows on one line and starts on another.

Match each line with what it does.

document.querySelector("#title")
document.querySelector(".note")
title.textContent = "Moved";
console.log(title.textContent);

Why is the script below the heading and not above it?

This stops with an error: Cannot set properties of null. Click the line that is the real cause.

ModifyChange the code on purpose and check the result.

Modify

Change the note to say you should bring sunscreen too.

Need a hint?

The note has class note, so its selector starts with a dot.

Another hint

const note = document.querySelector(".note"); then note.textContent = "…";

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.

Make the heading crimson.

Need a hint?

Style properties are on the element's style: title.style.color.

Another hint

The value is text, in quotes: title.style.color = "crimson";

Show the expected result
What the finished page looks like
What it should look like. Your words can be different.
Try it yourselfYour edits are saved in this browser.

Change the note's words to bring sunscreen too.

    This ticks when your page has all of the above.

    Make the heading crimson.

      This ticks when your page has all of the above.

      MakeBuild something new using what you have learned. Expect a few tries; that is normal.

      Change three things

      Find three elements and change something about each: text, colour (style.color), size (style.fontSize = "24px"). Add more elements of your own to the page if you like.

      Try it yourselfYour edits are saved in this browser.

      Info

      Where this leads

      Your code can now find and change anything on a page. Next: make it happen when someone clicks.

      You want the heading to change only when someone clicks a button. What is missing?

      For teachers

      Students find an element with querySelector, change its text and colour from JavaScript, read console.log in the Console, meet null from a mistyped selector, and change three elements on a page of their own. About 50 minutes.

      Open the teacher guide: answers, solutions, slides and a worksheet.

      Students will be able to

      Before class

      Read the unit primer first. Students know HTML and CSS (and, from p5.js, variables and functions); this is the first time JavaScript meets a page. document.querySelector takes a CSS-style selector as text: #id, .class, or a tag. textContent is the words, style.color and style.fontSize the look. The script sits below the elements it finds: it runs when the browser reaches it. The bug task is the most common slip of the whole course: a selector that matches nothing returns null, and the error appears on the next line. The playground says so in its hint. Our playground runs the whole page again each time you press Run, so changes made by the script always start from the HTML.

      Running the lesson

      1. Predict: ask what the page looks like before the script runs, and after.
      2. Run: point out the Console under the page and where the message came from.
      3. Investigate: for the bug, ask 'what does querySelector give you when it finds nothing?'
      4. Modify: ask students to say the selector aloud: 'hash for id, dot for class'.
      5. Make: ask each student which element they found first and how they knew its selector.

      The hard part (on purpose)

      The selector is text and a typo gives null, with the error one line later. Teach 'null means check the selector' before anything else.

      When students get stuck

      Stretch and support

      Stretch: Change the page title in the browser tab with document.title, or change an element's background with style.backgroundColor.

      Support: Give the first two lines (find, then change) and ask students to copy the pattern for each other element.

      Where it leads

      Next lesson: events, so the change happens when someone clicks.

      What each step is for

      Now you can
      Next lesson2. Click eventsRun a function when someone clicks a button: addEventListener, and the difference between handing over a function and running it.