Lesson 8 ยท about 15 minutes
โก JavaScript: Turn On the Electricity
Your house has walls (HTML) and paint (CSS). Today you flip the power switch: JavaScript makes things HAPPEN when you click. Your page comes alive!
๐ฌ Watch first (90 seconds)
The third superpower
Remember the house?
- HTML = the walls โ what's on the page.
- CSS = the paint โ how it looks.
- JavaScript = the electricity โ it makes things HAPPEN.
Without electricity, a light switch is just a plastic brick on the wall. Click it โ nothing. With electricity, click = light! JavaScript is the wire that connects a click to an action.
You only need two bricks and one spell:
- A
<button>Click me</button>โ a brick that WANTS to be clicked. But right now it's not wired up. - Give it an
id, like<button id="magic">. Anidis like aclass, but for ONE unique brick โ like a name tag. Two bricks can wear the same class, but an id belongs to one brick only. - A
<script>element at the very END of the body โ that's where the wiring goes.
const btn = document.getElementById("magic");
btn.addEventListener("click", function () {
document.getElementById("message").textContent = "You clicked!";
});
In kid language:
- Line 1: "Hey browser, find the brick named
magicand remember it asbtn." - Line 2: "When someone clicks that brick, do the following..."
- Line 3: "...find the brick named
messageand change the words inside it to You clicked!"
myshop.html file and click the button in your real browser.
๐ ๏ธ Build it (the fun part)
- Open
practice/myshop.htmlin VS Code. - Inside the
<body>, where you want the button, add these two bricks (type them, don't copy-paste!):
The<button id="buy">Buy now</button> <p id="message"></p><p>is empty on purpose โ it's a blank sign waiting for words. - Just BEFORE the closing
</body>tag, add the wiring (swap in YOUR product name!):<script> const btn = document.getElementById("buy"); btn.addEventListener("click", function () { document.getElementById("message").textContent = "๐ Thanks for buying Robo Sneakers!"; }); </script> - Save (โS), then open
myshop.htmlin your browser and refresh (โR). - Click Buy now. Did the thank-you message pop in? YOU JUST WIRED ELECTRICITY INTO YOUR HOUSE. โก๐
๐น๏ธ Try it right here โ the lesson checks your code!
Remember: the preview box can't run clicks, so the button will look sleepy there. Write the code anyway and press Check my code โ โ the checker reads your spell and tells you if it's right.
๐จ Now break it!
Do these in your real myshop.html file, in the real browser. Predict what will happen BEFORE you refresh!
- Meet the secret console. In your browser, right-click the page โ Inspect โ click the Console tab. This is where the browser whispers its problems. Now change
getElementById("buy")togetElementById("bye"), save, refresh, click the button. Nothing happens โ but look in the Console: a RED error message! The browser is telling you exactly what went wrong. Fix the id back. - Change the
textContentmessage to something totally silly. Save, refresh, click. Your button now says whatever YOU want. - Power move: make the button paint the whole page instead. Change the line inside the function to:
Click the button โ the whole page turns gold! JavaScript can change ANYTHING on the page: words, colors, sizes, everything.document.body.style.backgroundColor = "gold";
<script> at the END of the body (after the button)? Did you save before refreshing? And do the ids match EXACTLY โ buy and buy, not buy and Buy? Computers are very picky about spelling.
๐ฎ Quiz time
No peeking at the notes above โ trying to remember is what makes it stick!