JavaScript 11 · Put Pip’s report on the page

JavaScript lesson 11 of 50 · Early intermediate · About 25 minutes

Move Pip’s mission out of the console and onto a real web page.

Pip pauses where stone paths branch toward a crystal-roof workshop, a hanging garden and a distant beacon.
Mission Show the rover’s location in an HTML paragraph using JavaScript.

Get ready

New chapter. Put Pip’s mission on a page with controls and feedback. Start with this complete version of Pip’s mission, then compare it with the last chapter’s project.

Open Complete checkpoint below. For a browser preview, put the HTML in your editor’s HTML panel and the JavaScript in its JavaScript panel. For a local file, put the HTML first in a new .html file, then put the JavaScript between <script> and </script> at the end. Save the file and open it in a browser.

Match the HTML to the JavaScript. HTML tags create the game’s controls and text. An id names one element. The code uses that name to find the element. A class groups elements for styling, while a data- attribute can carry a game value. A status area with aria-live lets compatible screen readers announce updates.

Mission fact. A variable stores where Pip is. → Find the paragraph. Match #report to the element’s ID. → Show the result. textContent changes the displayed words.
Mission fact → Find the paragraph → Show the result

Understand the JavaScript in this lesson

Language to look for HTML elements and IDs, document, querySelector, textContent, program data and display.

1. Read the HTML first

The heading introduces the mission. The paragraph begins with placeholder text. Its id gives it a name that JavaScript can use. Add the lesson’s HTML fragment to the page and put the script after the fragment, or load the script with defer. The paragraph needs to exist before JavaScript tries to find it.

2. Keep the mission fact in a variable

destination holds the place where Pip has arrived. It is a string because the letters are inside quotation marks. The keyword const keeps this name pointing to the same value during this run. Keeping the place name separate lets you change the mission without rewriting the entire sentence.

const destination = "Sky Dock";

3. Find the paragraph

document is JavaScript’s way into the current web page. querySelector() finds the first element matching the selector inside the parentheses. The # means look for an ID, so #report finds the paragraph with id="report". The variable report now refers to that element.

const report = document.querySelector("#report");

4. Show the result as text

The dots reach properties or methods on an object. Assigning a string to report.textContent changes the words inside the paragraph, while leaving the paragraph itself in the page. The plus signs join strings. Notice the extra space after reached, which keeps the words separated.

report.textContent = "Pip has reached " + destination + ".";
Follow what happens step by step
  1. Load the page.
    The paragraph and heading are created.Why The browser reads the HTML before running the script.
  2. Run the script.
    Pip has reached Sky Dock.Why The script finds the paragraph and joins three pieces of text.

Predict before you run If the selector were #message, would the report change?

Reveal the answer

That ID does not exist in this fragment. The lookup gives null, so setting its textContent raises an error. Match the selector to the actual ID.

Complete checkpoint

HTML · the page and controls

<section aria-labelledby="mission-title">
  <h2 id="mission-title">Sky Island Rescue · Arrival</h2>
  <p id="report">Preparing the arrival report…</p>
</section>

JavaScript · the instructions

const destination = "Sky Dock";
const report = document.querySelector("#report");

report.textContent = "Pip has reached " + destination + ".";

Read the JavaScript line by line

Open a small group of lines while you read the complete checkpoint. Blank lines separate ideas. Closing braces mark the end of a block. An ellipsis here means you can read the rest in the full program.

Lines 1–4
Line 1 const destination = "Sky Dock";
Set up destination. Keep this name attached to the same value.
Line 2 const report = document.querySelector("#report");
Set up report. Keep this name attached to the same value. Find the first element matching the CSS selector. A missing match gives null.
Line 4 report.textContent = "Pip has reached " + destination + ".";
Plus adds numbers or joins text, depending on its values. Replace the element’s displayed text. The browser treats the value as text.

Mission challenge

Change the destination to Workshop and predict exactly what the page will say.

Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.