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.
Load these into Waypoints Prepare: choose Load URL and paste the copied address, or download the file and import it. In Prepare, the worksheet's Teacher edition shows the answers; the student copy hides them.
Predict and Run 10 min · Investigate 15 min · Modify 10 min · Make 15 min
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.
The selector is text and a typo gives null, with the error one line later. Teach 'null means check the selector' before anything else.
| What you will see | A question to ask | The fix |
|---|---|---|
| Cannot set properties of null. | Does the selector match the id or class exactly, with | Copy the id or class from the HTML, and add |
| Nothing changes, and there is no error. | Did you press Run, and is the change on a line after the selector? | Press Run again, and check the line that changes the element is inside the script. |
| The colour or size does not change. | Is the value in quotes and spelled as CSS spells it? | Write el.style.color = "red"; and el.style.fontSize = "24px"; (capital S in fontSize). |
| The page shows the script as text. | Is the code inside <script> and </script>? | Wrap the JavaScript in script tags. |
One possible solution for each task. Other correct programs are fine.
<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, water and sunscreen.";
</script>
<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";
title.style.color = "crimson";
</script>
<h1 id="title">My page</h1>
<p class="note">Change me with JavaScript.</p>
<p id="extra">Me too.</p>
<script>
const title = document.querySelector("#title");
title.textContent = "Welcome to my page";
title.style.color = "teal";
const note = document.querySelector(".note");
note.textContent = "Written by JavaScript.";
note.style.fontSize = "24px";
const extra = document.querySelector("#extra");
extra.textContent = "Me too, in a different colour.";
extra.style.color = "darkorange";
</script>
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.
What does querySelector give you if nothing matches?
null, which has no textContent: that is why the next line fails.
Next lesson: events, so the change happens when someone clicks.