Counter
What you'll build
A small HTML page with a number display and three buttons (+, −, Reset). Clicking a button updates the number on the page using JavaScript.
Goals
- Select DOM elements with getElementById
- Attach click handlers with addEventListener
- Update an element's text content from JS
- Keep application state in a variable
Prerequisites
- Variables and assignment
- Functions
- Basic HTML structure
Steps
- 1
Write the HTML
Create
counter.htmlwith a span for the count and three buttons. Give each element an id so JavaScript can find it.Code<span id="count">0</span> <button id="inc">+</button> <button id="dec">−</button> <button id="reset">Reset</button>Expected result
A number 0 and three buttons appear on the page.
- 2
Select the elements and set up state
In a script tag, select each element by id and declare a
countvariable starting at 0. State lives in JS; the DOM just displays it.Codeconst countEl = document.getElementById("count"); const incBtn = document.getElementById("inc"); const decBtn = document.getElementById("dec"); const resetBtn = document.getElementById("reset"); let count = 0;Expected result
No visible change yet, but JS now has references to every element.
- 3
Wire the increment button
Add a click listener to the + button. Inside, increment
countand setcountEl.textContentto the new value. This is the core update pattern: change state, then render.CodeincBtn.addEventListener("click", () => { count++; countEl.textContent = count; });Expected result
Clicking + increases the displayed number by 1 each time.
- 4
Wire decrement and reset
Add listeners for the other two buttons. Decrement subtracts 1; reset sets count back to 0. Always update textContent after changing state.
CodedecBtn.addEventListener("click", () => { count--; countEl.textContent = count; }); resetBtn.addEventListener("click", () => { count = 0; countEl.textContent = count; });Expected result
− decreases the number; Reset returns it to 0.
Hint
If nothing happens on click, check that the ids in JS exactly match the ids in HTML.
Try it yourself
<span id="count">0</span>
<button id="inc">+</button>
<button id="dec">−</button>
<button id="reset">Reset</button>
<script>
// TODO: select elements, set count = 0, wire three listeners
</script><span id="count">0</span>
<button id="inc">+</button>
<button id="dec">−</button>
<button id="reset">Reset</button>
<script>
const countEl = document.getElementById("count");
const incBtn = document.getElementById("inc");
const decBtn = document.getElementById("dec");
const resetBtn = document.getElementById("reset");
let count = 0;
incBtn.addEventListener("click", () => {
count++;
countEl.textContent = count;
});
decBtn.addEventListener("click", () => {
count--;
countEl.textContent = count;
});
resetBtn.addEventListener("click", () => {
count = 0;
countEl.textContent = count;
});
</script>What you learned
You practiced the fundamental DOM pattern: select elements, keep state in a variable, listen for events, and update the DOM after each state change.
Related Resources
Related Lessons
Related Practice
- DOM Events MCQ
Quiz on event listeners and handler functions.
- Variables Predict
Predict the value of a variable after reassignment.
Related Tools
- HTML/CSS/JS Minifier
Minify your counter page to see how production HTML is shipped.
Related Cheatsheets
- JavaScript Cheatsheet
DOM methods and event syntax reference.
Related Reference
- JavaScript Array Methods
Complete array method reference.
Related Errors
- Cannot Read Properties of Null
Null element selection mistakes.
- Type Error Not a Function
Calling a non-function value.
Related Glossary
- DOM
Document Object Model basics.
- Event
User and system events.
- Event Listener
Handling events in JS.
- Variable
Variable assignment and state.
Related Glossary
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
- DOM
Document Object Model basics.
- Event
User and system events.
- Event Listener
Handling events in JS.
- Variable
Variable assignment and state.
Related Errors
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
- Cannot Read Properties of Null
Null element selection mistakes.
- Type Error Not a Function
Calling a non-function value.