JavaScript lesson 13 of 50 · Early intermediate · About 30 minutes
Give Pip three destinations and teach one function to tell the buttons apart.

Get ready
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.

Understand the JavaScript in this lesson
Language to look for querySelectorAll, attribute selectors, data attributes, event.currentTarget, dataset, for...of.
1. Put information on the controls
Each destination button contains a data-destination attribute. It stores developer-chosen text connected to that element. The on-screen label can include a symbol. The data stays simple and consistent, even when you change what the button looks like or how you label it.
2. Find the group of buttons
The selector [data-destination] matches elements carrying this attribute. querySelectorAll() returns a list-like group of every match. The for...of loop takes one element per turn, calls it button and attaches the same listener to it.
const buttons = document.querySelectorAll("[data-destination]");
3. Read the control that owns the listener
When a click happens, the browser passes an event object into choose. Its currentTarget is the element whose listener is running. If you click the little symbol inside Workshop, target may be that inner span. currentTarget is still the button carrying the destination.
const destination = event.currentTarget.dataset.destination;
4. Keep text and behaviour together
The dataset property gives JavaScript access to the element’s data attributes. The value of data-destination is available as dataset.destination. The function then makes a sentence and assigns it to textContent. A single change to the sentence now applies to every destination.
report.textContent = "Pip plots a course to " + destination + ".";-
Click Garden.
Pip plots a course to Garden.Why The Garden button supplies its own destination data. -
Click the symbol inside Workshop.
Pip plots a course to Workshop.Why currentTarget still identifies the registered button.
Predict before you run Would changing the visible word Workshop to Repair shop change the report?
Reveal the answer
The report still uses Workshop while data-destination says Workshop. The listener reads the stored value.
Complete checkpoint
HTML · the page and controls
<section aria-labelledby="mission-title">
<h2 id="mission-title">Sky Island Rescue · Course selector</h2>
<p id="report" role="status">Choose Pip’s destination.</p>
<button type="button" data-destination="Workshop"><span aria-hidden="true">⚙</span> Workshop</button>
<button type="button" data-destination="Garden">Garden</button>
<button type="button" data-destination="Beacon">Beacon</button>
</section>
JavaScript · the instructions
const report = document.querySelector("#report");
const buttons = document.querySelectorAll("[data-destination]");
function choose(event) {
const destination = event.currentTarget.dataset.destination;
report.textContent = "Pip plots a course to " + destination + ".";
}
for (const button of buttons) {
button.addEventListener("click", choose);
}
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–10
-
Line 1
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 2
const buttons = document.querySelectorAll("[data-destination]"); - Set up buttons. Keep this name attached to the same value. Find all elements matching the CSS selector.
-
Line 4
function choose(event) { - Define choose. The body runs when the function is called, with inputs named event.
-
Line 5
const destination = event.currentTarget.dataset.destination; - Set up destination. Keep this name attached to the same value.
-
Line 6
report.textContent = "Pip plots a course to " + destination + "."; - Plus adds numbers or joins text, depending on its values. Replace the element’s displayed text. The browser treats the value as text.
-
Line 9
for (const button of buttons) { - Take one value at a time from this sequence and run the body with it.
-
Line 10
button.addEventListener("click", choose); - Register a function the browser should call when the named event happens.
Mission challenge
Add a Sky Dock button with its own data attribute and confirm it works with the same function.
Change one piece at a time. Run the checkpoint again and compare what you expected with what actually happened.