Make a page respond: a click counter
The smallest interactive program — a button that counts. Listen, change, render.
A button that increments a number on screen each time you click it.
Variables, selecting an element, listening for a click, and updating the DOM.
Prerequisites
A browser. A first look at HTML helps but is not required.
A web page becomes interactive the instant JavaScript listens for something the user does and changes the page in response. The whole loop fits in a click counter.
<!doctype html><html lang="en"> <head><meta charset="utf-8" /><link rel="stylesheet" href="styles.css" /></head> <body> <main> <output id="count">0</output> <button id="inc">Count</button> </main> <script src="script.js"></script> </body></html>body { font-family: system-ui; display: grid; place-items: center; min-height: 100vh; margin: 0; }output { font-size: 4rem; font-variant-numeric: tabular-nums; display: block; text-align: center; }button { font-size: 1.25rem; padding: 0.5rem 1.5rem; border-radius: 9px; border: 1px solid #ccc; cursor: pointer; }let count = 0;const output = document.getElementById('count');const button = document.getElementById('inc'); button.addEventListener('click', () => { count += 1; output.textContent = count;});Event listeners
`element.addEventListener("click", fn)` asks the browser to run `fn` every time the element is clicked. That is how your code hears the user.
button.addEventListener('click', () => { count += 1; output.textContent = count;});Milestone
You made the page respond to a human.
Listen for an event, change some state, render it. Almost everything interactive on the web — forms, games, chat — is that same loop, repeated.
Tutorial
Next: build a calculator
Same shape, more concepts — conditionals and functions arrive when the calculator needs them.
Concepts covered · 3
More like this
Build interaction with AI
The vibe-coder path for JS events: the event→state→render loop you must hold in your head, the prompt, and the review.
Build a script with AI
The vibe-coder path for Python: the shape of a small program, the prompt, and the edge cases AI forgets.
Build a program with AI
The vibe-coder path for C++: the few things you must understand to judge AI-written C++, the prompt, and the review.
Liked this one?
Pass it on
Discussion
Be the first to ask
Your questions stay private with the team. We can pin answers to share with everyone.
Sign in to ask a question privately on this tutorial.
Sign inNo pinned answers yet for this tutorial.