A is a list of instructions that a computer follows in order, one after another. That is all. A program that runs a bank and a program that makes a page say “You clicked!” are made the same way: small, exact steps, carried out from the top down.
A good picture of a program is a recipe card. The card doesn’t cook anything by itself. Someone has to read it, starting at the top, doing each step before moving to the next. In your game, that someone is the browser. When it opens game.html, it reads the file from the first line to the last and does what each line says: show this heading, show this button, and so on.
Two things about recipes are true of programs too. The order matters: you can’t ice a cake before you bake it, and a script can’t find a button the browser hasn’t read yet. And the steps must be exact. A cook can guess what “a pinch” means. A computer can’t guess at all. If an instruction says guessButton and the page says guesbutton, the computer won’t notice the resemblance. It does exactly what is written, which is why a single wrong letter can stop a program.
There is a third thing recipes and programs share: someone has to write them down first, in words the reader understands. A recipe in a language the cook can’t read is useless, however good the food. Programs are the same, which is why programming languages exist: they are ways of writing instructions that a computer can read without guessing.
Last part, your file held only HTML, which describes things to show. In this part you add the other kind of instruction: JavaScript, which tells the browser what to do. You will write three lines of it. They are the most important lines in the series, because every feature after this one is built the same way.
Your page needs two new elements. A <button> element draws a button the player can click. A <p> element is a paragraph, a line of ordinary text; yours starts empty and waits for something to say.
The script has to be able to find these two elements among everything on the page, so each one gets a name tag, called an . The button’s is guessButton and the paragraph’s is message. Inside the start tag it looks like this: <button id="guessButton">. The capital B matters. To the computer, guessButton and guessbutton are two different names.
JavaScript lives in a , written between <script> and </script>. Here is the whole of yours:
document.getElementById("guessButton").addEventListener("click", function () { document.getElementById("message").textContent = "You clicked!";});It looks dense, so read it as a sentence: find the guess button; when it’s clicked, do this: find the message and set its text to “You clicked!”
That middle part, “when it’s clicked”, is new. A click is an : something that happens on the page, at a moment nobody knows in advance. Your script doesn’t wait around for it. It runs once, from top to bottom, and leaves an instruction with the button: whenever you’re clicked, run these lines. Then, every time the player clicks, the button sends a signal and the saved lines run, reaching back into the page to change the paragraph.
Open game.html in your editor. Each step below shows only the part of the file that changes, with the new lines marked.
Step 1. Add the button and the empty paragraph inside the body, under the heading:
<body> <h1>Guess the Number</h1>
<button id="guessButton">Guess</button>
<p id="message"></p></body>Notice that the button’s label, Guess, sits between its tags, just like the words of the heading. The empty lines between elements are only there to make the file easier for you to read; the browser ignores them, just as it ignores the spaces at the start of lines.
Save and refresh. A button appears. Clicking it does nothing yet, because nothing has told it what to do. Saving and checking after each step is a good habit: if something breaks, you know it was the last thing you changed.
Step 2. Add the script, just before </body>:
<body> <h1>Guess the Number</h1>
<button id="guessButton">Guess</button>
<p id="message"></p>
<script> document.getElementById("guessButton").addEventListener("click", function () { document.getElementById("message").textContent = "You clicked!"; }); </script></body>Type it rather than copying it, and watch the punctuation as you go. The dots join one step to the next. The quotes mark text: “guessButton” is a name to look for, not an instruction. Every opening bracket, round ( or curly {, needs its closing partner, and the last line, });, closes both the curly bracket and the round one that opened on the first line. The semicolon ends an instruction, like a full stop.
The script goes at the end of the body for a reason. The browser reads from the top, so by the time it reaches the script, the button and the paragraph already exist and getElementById can find them. Keep your script at the end of the body for the whole series.
Here is the full file so far:
<!doctype html><html lang="en"><head> <meta charset="utf-8"> <title>Guess the Number</title></head><body> <h1>Guess the Number</h1>
<button id="guessButton">Guess</button>
<p id="message"></p>
<script> document.getElementById("guessButton").addEventListener("click", function () { document.getElementById("message").textContent = "You clicked!"; }); </script></body></html>Save the file and refresh the browser. Remember the rhythm: edit, save, refresh. Your page should look like the preview below. Click the button in yours and in the preview, and compare.
Your game at the end of part 2
This preview runs the full file above. Click Guess. The refresh arrow reloads the page, as refreshing your own file would.
Guess the Number
The preview uses the site's colours and fonts; your page uses the browser's plain defaults. The behaviour is the same.
If the preview works and your page doesn’t, the difference is somewhere in your file. Put the two side by side and compare them line by line, starting from the script. Most differences are a single character: a missing quote, a lower-case b, a bracket in the wrong place. If you can’t find it, copy the full file from the end of the last section with its copy button, paste it over yours, save, and check that it works. Then look back at what changed.
Under the heading there is a Guess button and nothing else. When you click it, the words You clicked! appear under it. Clicking again changes nothing you can see, because the paragraph already says You clicked!, and refreshing the page empties it again.
Now make a mistake deliberately, so you know what one looks like. In the script, delete one letter s from guessButton, so the first line asks for getElementById("guesButton"). Save, refresh and click. Nothing happens.
Open the console. Chrome and Edge show a red message like this:
Uncaught TypeError: Cannot read properties of null (reading 'addEventListener') game.html:15It reads oddly at first, so take it piece by piece:
- TypeError is the kind of problem: the code tried to use a value in a way that kind of value doesn’t allow.
- Cannot read properties of null says what went wrong.
getElementByIdlooked for an element called guesButton, found none, and handed backnull, JavaScript’s word for “nothing here”. - (reading ‘addEventListener’) says what the code tried to do with that nothing: attach a click listener to it. You can’t attach anything to nothing.
- game.html:15 is where: line 15 of your file. Click it and the browser shows you the line.
Firefox and Safari word the same problem differently, with phrases such as “is null” or “null is not an object”, but they point at the same line. The fix is to put the s back, save and refresh. Click again, and the page answers.
This message, about something being null, is one you’ll see again. It almost always means the script asked for an element by a name the page doesn’t have.
Make the button say something else. Change “You clicked!” to a message of your own, save and refresh.
Then, if you’d like more, add a second button, with its own id and its own message, that writes into the same paragraph. You’ll need a new <button> element and a second listener in the script. Notice how much of the second listener is the same as the first.
Everything you need is already in your file. The pattern is the one you’ll use for the rest of the series: find an element by its id, listen for an event, and change something on the page. Try it before you open the solution, and remember to save and refresh after every change. If a button does nothing, the console is the first place to look.
Show a solution
Add the button under the first one:
<button id="guessButton">Guess</button><button id="helloButton">Say hello</button>And a second listener inside the script, after the first:
document.getElementById("guessButton").addEventListener("click", function () { document.getElementById("message").textContent = "You clicked!";});
document.getElementById("helloButton").addEventListener("click", function () { document.getElementById("message").textContent = "Hello!";});Each button listens for its own clicks. Remove the second button before part 3 if you want your file to match the series exactly.
Your page answers a click, but it always says the same thing. In part 3 the game gets a memory: a secret number, kept in a labelled box called a variable.
Sources for this part
HTML elements
<button>: The Button element, MDN Web Docs. Checked 16 October 2026.<p>: The Paragraph element, MDN Web Docs. Checked 16 October 2026.- id, MDN Web Docs. Checked 16 October 2026.
<script>: The Script element, MDN Web Docs. Checked 16 October 2026.
JavaScript
- Document: getElementById() method, MDN Web Docs. Checked 16 October 2026.
- EventTarget: addEventListener() method, MDN Web Docs. Checked 16 October 2026.
- Element: click event, MDN Web Docs. Checked 16 October 2026.
- Node: textContent property, MDN Web Docs. Checked 16 October 2026.
- What is JavaScript?, MDN Web Docs. Checked 16 October 2026.
- TypeError: “x” is (not) “y”, MDN Web Docs. Checked 16 October 2026.
- TypeError, MDN Web Docs. Checked 16 October 2026.
- Document Object Model (DOM), MDN Web Docs. Checked 16 October 2026.
History
- JavaScript, MDN Web Docs glossary. Checked 16 October 2026.
- Document Object Model (DOM) Level 2 Events Specification, W3C Recommendation, 13 November 2000. Checked 16 October 2026.