Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Run Code on Every Click

In this video, you gave Priya’s page a Count me in button, handed the browser a function to call when it’s clicked, and watched the script finish before that function ever ran.

Key Points

  • A <button> is the HTML element for an action. A guest can click it, or reach it with the Tab key.
  • No line of your script can call a function at the moment a guest clicks, because the script can’t know when that will be. Instead, you hand the function to the browser.
  • Every element has a method, addEventListener(), that takes two arguments: the name of what to listen for, here the string "click", and the function to call.
  • A first attempt many people write is a detour: countMeInButton.addEventListener("click", countMeIn());. The parentheses call countMeIn() right there, while that line runs, so the Console shows Count me in clicked, then main.js finished, though nobody has clicked. Clicks then log nothing more, and there’s no error.
  • The call’s result, undefined, is what addEventListener() receives, so it has nothing to keep with the button. With forEachGame(), this slip threw a TypeError. Here, addEventListener() kept nothing, so a click has no function to run, and the browser stays quiet.
  • Without the parentheses, addEventListener("click", countMeIn) hands over the function itself. The script runs from top to bottom, the browser keeps countMeIn with the button, and nothing calls it. The script reaches its last line, logs main.js finished, and finishes.
  • Each click then makes the browser call countMeIn(), and Count me in clicked appears in the Console. Chrome stacks identical lines, so after three clicks you see one line with a 3 beside it.
  • The click is an event, something that happens on the page, and countMeIn() is its handler, the function the browser calls for it. In addEventListener(), listener means the same thing.
  • Watch out for the event’s name. It’s "click", in lowercase, not "onclick", which names an HTML attribute, not an event. With "onclick" here, there’s no error, but no click ever calls your function.
  • addEventListener() hands the browser your function, with no parentheses after its name. Your script runs to its end first. Then the browser calls the function, once for every click.

The Final Code

In scratch/index.html, the button sits in main, under the players list:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 So far, every function in Priya’s script has run because a line of main.js called it. In this video, you’ll hand a function to the browser, and the browser will call it each time a guest clicks a button.

00:12 If you don’t have Priya’s files, the text below has a version for a blank tab.

00:17 Priya wants guests to tell her they’re coming. In index.html, under the players list, add a button element with the text Count me in. Give it the id count-me-in, so the script can find it.

00:30 A button is the HTML element for an action. A guest can click it, or reach it with the Tab key. At the end of main.js, delete the vote test’s log line. Then add a new last line that logs main.js finished, so you can see when the script is done.

00:48 Save and reload the page. The button appears under the players, and main.js finished appears in the Console. Click the button, and nothing happens.

01:00 The page needs a function that runs on every click. But no line of your script can call it at the right moment, because the script can’t know when a guest will click.

01:10 Write that function first. countMeIn is an arrow with no parameters, stored in a const, and for now its body logs Count me in clicked. Above it, find the button, the way you found the players list.

01:23 Store it as countMeInButton in a const.

01:27 Every element has a method called addEventListener, made for this. It takes two arguments: the name of what to listen for, here the string click, and the function to call.

01:39 Here’s a first attempt many people write, and it’s a detour. You call functions with parentheses all the time, so even after forEachGame, they slip in here too.

01:48 Your turn: pause the video and predict what the Console shows after the page loads and you click three times.

02:00 Save and reload, and the Console shows two lines: Count me in clicked, then main.js finished.

02:09 It says clicked, but nobody has clicked yet. Now click three times. Nothing more appears, and there’s no error.

02:17 The parentheses call countMeIn right there, while that line runs. So it logs once, as the page loads. The call’s result, undefined, is what addEventListener receives, so it has nothing to keep.

02:31 With forEachGame, this slip threw a TypeError right away. Here, addEventListener kept nothing, so a click has no function to run, and the browser stays quiet.

02:42 So delete the parentheses, and hand over the function itself. Save and reload, and this time main.js finished is the only line in the Console. The script ran from top to bottom.

02:57 At addEventListener, the browser took countMeIn and kept it with the button. Nothing called it. Then the script reached its last line, logged, and finished.

03:08 Now click. The browser calls countMeIn, and Count me in clicked appears in the Console. Click twice more, and it runs twice more. Chrome stacks identical lines, so you see one line with a 3 beside it.

03:23 The click is an event, something that happens on the page, and countMeIn is its handler, the function the browser calls for it. In addEventListener, listener means the same thing.

03:36 Watch out for the event’s name. It’s click, in lowercase, not onclick, which names an HTML attribute, not an event. With onclick here, there’s no error, but no click ever calls your function.

03:52 So here’s the rule. addEventListener hands the browser your function, with no parentheses after its name. Your script runs to its end first. Then the browser calls the function, once for every click.

04:06 Priya wants more than a log line, though. Each click should add one to a headcount on the page, and that count has to last from one click to the next. That’s what you’ll build in Keep Track Between Clicks.

Become a Member to join the conversation.