Show me: the three steps of every spell
Almost everything JavaScript does on a page follows three steps:
- Find an element on the page (a button, a heading, an image…),
- listen for an event (a click, hovering the mouse, typing text…),
- 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:
id="my-button": a nametag for the element, similar to a class, but anidshould appear only once on the page. JavaScript uses it to find the element.const: a box with a name (a variable), where we store the element we found. More about variables next time.document.getElementById("..."): “page, find me the element with this id.”addEventListener("click", () => { ... }): “when a click happens, run whatever is inside the curly braces.” That funny arrow() => {}is called a function: a package of code that runs later.textContent: the text inside an element. When you assign it new text, it changes on the page instantly.
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
- Every spell: find an element → listen for an event → do something.
document.getElementById("id")finds an element,addEventListener("click", () => {...})waits for a click.textContentchanges an element’s text instantly, with no page reload.
© 2026 Ing. Martin Polak / AlgoRhino · Content usage terms