Lesson 4 ยท Make a web page
Buttons that do things
Use JavaScript to make a button that counts every click.
About 20 minutes
Tab indents. Press Escape, then Tab, to leave the editor.
Your page has bones (HTML) and paint (CSS). Now it's time to give it a brain! ๐ง In this lesson you'll use JavaScript to make a button that counts every time you click it.
JavaScript is the brain
JavaScript is a language for giving the browser instructions. HTML says what's on the page, CSS says how it looks, and JavaScript says what to do.
A computer follows instructions exactly, in order, one at a time. It's like a robot chef ๐ค that follows a recipe word for word. If the recipe says "add salt", it adds salt. If you forget to write "turn on the oven", the robot will never turn it on!
Saying hello
Here's one instruction:
console.log('Hello!')
This tells the browser to write Hello! in the console. The console is a special message box for coders. In the playgrounds, it's under the page. ๐ฌ
The words inside the quote marks ' ' are called a string. A string is just a bit of text, like letters on a string of beads. ๐ฟ
Boxes with names
To count clicks, the computer needs to remember a number. It stores things in variables. A variable is like a box with a name written on it.
let count = 0
This makes a box called count and puts the number 0 inside. Later you can put a new number in the same box:
count = count + 1
Read it like this: "Take what's in the count box, add 1, and put the answer back in the box." If count was 0, now it's 1. Do it again and it's 2.
We use let when the value inside the box will change. We use const for boxes whose value stays the same forever.
Finding things on the page
Before JavaScript can change something on the page, it has to find it. That's what document.querySelector does. Document means the whole page.
const button = document.querySelector('#add')
This says: "Go find the part of the page with the id add, and keep it in a box called button." The # means "look for an id". An id is a name tag you give to one element in your HTML, like <button id="add">. ๐ท๏ธ
Listening for clicks
Now the clever part. We can ask the button to listen for clicks, like a doorbell waiting to be pressed. ๐
button.addEventListener('click', function () {
console.log('Ding dong!')
})
A function is a set of instructions wrapped up together. This one doesn't run straight away. It waits, and runs every single time someone clicks the button.
Changing the words on the page
Each element has words inside it, called its textContent. JavaScript can swap those words for new ones:
countText.textContent = count
This puts whatever number is in the count box onto the page, inside countText.
Make a click counter
You've got all the pieces. Let's put them together! ๐งฉ
Challenge
Make a click counter
The button already listens for clicks. Press Run, click the button, and watch the console say Click!.
Now make it count. Inside the function, under the two comments:
- Add 1 to
count. - Put
counton the page withcountText.textContent.
Press Run, then click the button to try it.
- Nothing logged yet.
- Clicking the button changes the number
- Each click adds exactly 1
You made a page that does something. That's real programming! ๐
Start over
A counter is more useful if you can set it back to zero. Let's add a second button.
Here's a puzzle: if you only change the words on the page back to 0, but forget to empty the count box, what happens on the next click? Try it and see! ๐ต๏ธ
Challenge
Add a "Start over" button
There's a new Start over button. It listens for clicks, but does nothing yet.
Make it set count back to 0, and then show count on the page. Make sure the next click on Click me! shows 1.
- Nothing logged yet.
- Clicking "Click me!" still counts up
- Clicking "Start over" puts the number back to 0
- After starting over, the next click shows 1
What you learned
- JavaScript gives the browser step-by-step instructions, like a robot following a recipe.
- A variable is a named box that holds a value.
let count = 0makes one. document.querySelector('#add')finds the element with the idadd.addEventListener('click', function () { โฆ })runs a function every time something is clicked.textContentchanges the words on the page, and you used it to build a working click counter! ๐
Finished this lesson?
Mark it complete to track your progress on this device.