← Level 2 – Wizard

Lesson 2 of 8 ⏱ 25 minutes

Your first spell: a button that does something

The page reacts to a click for the first time. You'll learn to find an element and listen for events.

Show me: the three steps of every spell

Almost everything JavaScript does on a page follows three steps:

  1. Find an element on the page (a button, a heading, an image…),
  2. listen for an event (a click, hovering the mouse, typing text…),
  3. do something (change the text, the color, hide it, show it…).

Here’s what that looks like in code. In the HTML, add a button and a paragraph:

<button id="my-button">Click me</button>
<p id="greeting">Nothing has happened yet.</p>

And in script.js:

// 1. Find the elements by their id
const myButton = document.getElementById("my-button");
const greeting = document.getElementById("greeting");

// 2. Listen for a click on the button
myButton.addEventListener("click", () => {
  // 3. Do something: change the paragraph's text
  greeting.textContent = "The spell works! 🎉";
});

Let’s break down the new stuff:

Your turn 💪

Add your first spell to your website. Pick one based on your project (or make up your own):

🏫 School website: a "What's for lunch today?" button

HTML (on the home page):

<button id="lunch-button">What's for lunch today?</button>
<p id="lunch"></p>

script.js:

const lunchButton = document.getElementById("lunch-button");
const lunch = document.getElementById("lunch");

lunchButton.addEventListener("click", () => {
  lunch.textContent = "Today: pizza and salad! 🍕";
});
🙋 Personal website: a "Tell me a joke" button
<button id="joke-button">Tell me a joke</button>
<p id="joke"></p>
const jokeButton = document.getElementById("joke-button");
const joke = document.getElementById("joke");

jokeButton.addEventListener("click", () => {
  joke.textContent = "Why do programmers prefer dark mode? Because light attracts bugs! 😄";
});
🌍 Travel diary: a "Where are we going next?" button
<button id="where-button">Where are we going next?</button>
<p id="where"></p>
const whereButton = document.getElementById("where-button");
const where = document.getElementById("where");

whereButton.addEventListener("click", () => {
  where.textContent = "Packing our bags: we're headed to the beach in Greece! 🌊";
});
⚽ Club website: a "When's the next match?" button
<button id="match-button">When's the next match?</button>
<p id="match"></p>
const matchButton = document.getElementById("match-button");
const match = document.getElementById("match");

matchButton.addEventListener("click", () => {
  match.textContent = "Saturday at 10:00, home game vs. Riverside. Come cheer us on! 📣";
});

Bonus: style your button in CSS so it looks nice:

button {
  font-size: 16px;
  font-weight: bold;
  padding: 10px 20px;
  border: none;
  border-radius: 999px;
  background-color: #1d4ed8;
  color: white;
  cursor: pointer;
}

button:hover {
  background-color: #1e40af;
}
⚠️

Not working? Open the console (F12). The most common mistakes: 1) the id in HTML doesn’t exactly match the name in getElementById (even capitalization matters), 2) the <script> isn’t at the very end of <body>, 3) a missing bracket: the console will tell you the line number.

✅

Check yourself: You click the button and text appears on the page. You just connected all three languages of the web: HTML (the button), CSS (the look), and JavaScript (the behavior).

What to take away from this lesson