▶ TrainingGet Pro
All tools & lessonsAll toolsAllGet the appApp
Learning departments Music, Math, Cookery, Programming and Art are open – explore →

JavaScript lesson 24: The web page: click events and a project

US version. US grades and US spelling. · UK version of this lesson

button.addEventListener("click", f) SAVES the function f. It runs every time the button is clicked – not when it is added. button.click() clicks it from code, which is handy for testing. A complete 50-minute JavaScript lesson for beginners (Grade 8), from Unit 9: The web page. It runs right here in your browser – nothing to install.

Time
50 minutes + 40 min homework
Grade
Grade 8 (ages 13–14)
You need
A computer or tablet, paper and a pencil
Graded
Mostly instantly; the project with our checklist

For parents: how to use this lesson

You don't need to know JavaScript yourself. The lesson explains each step on screen, the way Mr Babbage, our Head of Computing, likes it: a worked example first, then a go for your child. Each question shows a small web page and some code, and your child says what it does – graded straight away – and the web page they make at the end is judged by you, with our checklist below.

  1. Read what your child will learn together.
  2. Press Start the lesson below. Sit with your child for the teaching and the worked example (about 16 minutes): ask them to say what each line does.
  3. Let them do the practice on their own. When code goes wrong, the error message is a clue – let them read it out and try again before you help.
  4. Finish with the talk question: What would you add to your page next, and how would you test it?

Watch out for

  • addEventListener runs the function straight away. Add a console.log before and after – then click.
  • addEventListener("click", hello()) is right. That runs hello NOW. Pass hello.

Miss Lovelace's tip for home: ask your child to explain one line of their program to you in their own words. If they can explain it, they understand it – and every error message is a clue, not a failure.

What your child will learn

Learning goal: run code when an element is clicked.

Success looks like

  • I can run code when an element is clicked.
  • I can use the event object.
  • I can plan, build and test a small interactive page.

Key words

event · listener · handler · click · event object · test

What to have ready

  • A device with a web browser (the code runs in the browser – nothing to install)
  • Paper for tracing
  • A keyboard helps for typing code (a tablet or phone works too)

The big ideas (for the adult)

button.addEventListener("click", f) SAVES the function f. It runs every time the button is clicked – not when it is added. button.click() clicks it from code, which is handy for testing.

Pass the function itself, with no brackets. Two listeners on one button both run, in order. The handler can take an event: (e) => …, where e.target is the element clicked.

Project: plan a small page – a click counter, a to-do list or a quiz button. Write the steps, build one step at a time, and test every button.

Worked example – see it

// <h1 id="title">Our trip</h1>
// <p id="msg" class="note">Hello Ruby</p>
// <ul id="list"><li>frog</li><li class="star">tiger</li><li>koala</li></ul>
// <button id="go">Click me</button>

const b = document.querySelector("#go")
b.addEventListener("click", () => {
  console.log("Clicked!")
})
console.log("Ready")

It prints:

Ready

The lesson, step by step

50 minutes in 5 parts
PartTimeHow it is checked
Starter5 minGraded by Classfern
Teaching input8 minRead, watch and talk
See it, then predict8 minGraded by Classfern
Predict, trace and check22 minGraded by Classfern + teacher or parent, with our checklist
Closure and exit ticket7 minGraded by Classfern + self-check

Checklist for the project (teacher or parent)

The questions in this lesson are graded by Classfern. The web page your child makes at the end is judged by you, with this checklist – it is not graded by computer. For each line, choose the description that fits best.

  • Planning: the steps (what each button does) were written down before coding
    No plan → Some steps written → A clear plan that was followed
  • It works: clicking does what the plan says, every time
    Does not run → Runs, with some bugs → Works for normal use → Works, even when clicked many times or in an unusual order
  • Testing: every button and message was tried, including the edge cases
    Not tested → Tested once → Every part tested, including an edge case
  • Readable: clear names, helpful comments, and a page that is easy to use
    Hard to follow → Mostly clear → Clear names, comments and a tidy page throughout

The lesson

It runs here on the page. No sign-up and no names – progress is kept only in this browser. Each visit picks a new mix of questions.

Homework

40 minutes: 5 questions – predict what page code does, spot what goes wrong, and check the key ideas – then a short reflection, and finishing the web page. New questions every time, all graded instantly.

Practice tests

All JavaScript practice tests · All 24 JavaScript lessons

Curriculum

CSTA K–12 Computer Science Standards (2017), grades 6–8: 2-AP-13 – Decompose problems to help design, implement and review programs; 2-AP-17 – Systematically test and refine programs using a range of test cases.

The standard codes are CSTA's; the descriptions are our own words.

Our lessons and questions are our own. They are not written or endorsed by any government department, testing agency or exam board. JavaScript is a trademark or registered trademark of Oracle Corporation in the U.S. and other countries; Classfern is not affiliated with or endorsed by it.