Keep Track Between Clicks

In Run Code on Every Click, Priya’s Count me in button got a handler, countMeIn(), which the browser calls each time a guest clicks. So far, all it does is log a line. Priya wants a headcount on the page that goes up by one with every click.

In this lesson, you’ll make a value last from one click to the next and show it on the page after every click. Then you’ll give each guest a Check in button of their own, with a handler that knows whose button it is. You can follow along in Priya’s files or, if you don’t have them, in a blank tab where you type each step into the Console. Start with the headcount.

Keep a Count Between Clicks

For a headcount, you need a number that goes up on every click and a place on the page to show it. If you’re using a blank tab, you’ll first type a short setup that builds the button and paragraph. After that, each step comes first as a change to Priya’s files, then as the same step in the Console. Follow the route that matches what you have.

If you’re using a blank tab, open a new tab, go to about:blank, and open the Console with Ctrl+Shift+J, or Cmd+Option+J on macOS. Type these entries one at a time. They make the two pieces you’ll use, a Count me in button and an empty paragraph under it, and keep them in countMeInButton and headcountLine, as the updated script will:

Language: JavaScript
> const countMeInButton = document.createElement("button");
undefined
> countMeInButton.textContent = "Count me in";
'Count me in'
> document.querySelector("body").append(countMeInButton);
undefined
> const headcountLine = document.createElement("p");
undefined
> document.querySelector("body").append(headcountLine);
undefined

If you have Priya’s files, start with the place. In scratch/index.html, add an empty paragraph under the Count me in button, with the ID headcount:

Language: HTML
<ul id="players"></ul>
<button id="count-me-in">Count me in</button>
<p id="headcount"></p>

Now the number. At the end of scratch/main.js, find the new paragraph and keep it in headcountLine. Then replace the body of countMeIn() with code that counts. A count that starts at zero and goes up by one uses a let declaration and an assignment, so the natural place for both is the function that does the counting:

Language: JavaScript Filename: scratch/main.js
const countMeInButton = document.querySelector("#count-me-in");
const headcountLine = document.querySelector("#headcount");

const countMeIn = () => {
  let headcount = 0;
  headcount = headcount + 1;
  headcountLine.textContent = "Headcount: " + headcount;
};

countMeInButton.addEventListener("click", countMeIn);
console.log("main.js finished");

In the blank tab, type the same handler and hand it to the button. Press Shift+Enter between the lines of countMeIn:

Language: JavaScript
> const countMeIn = () => {
…   let headcount = 0;
…   headcount = headcount + 1;
…   headcountLine.textContent = "Headcount: " + headcount;
… };
undefined
> countMeInButton.addEventListener("click", countMeIn);
undefined

The last line of countMeIn() puts the count on the page, so you can see it change instead of reading the Console. With Priya’s files, save main.js, then reload the page so the script runs again. On either route, click Count me in three times and watch the paragraph.

After the first click, Headcount: 1 appears under the button. The next two clicks leave it at Headcount: 1. There’s no error, and with Priya’s files the Console shows only main.js finished.

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

Become a Member to join the conversation.