All labs
beginnerJavaScript
Make a Button Do Something
You will learn to
- Select elements from the Document Object Model (DOM)
- Listen for user interactions (click events)
- Dynamically update HTML content using JavaScript
- Use variables to track state (like click counts)
- Modify CSS classes and inline styles dynamically
Before you start
- Comfort editing HTML (try Build Your First Web Page first)
- The in-browser Code Sandbox (embedded below)
Your workspace
script.js
Loading editor...
Console
console.log output appears here.
Step-by-step
1. Set up the HTML structure
Done when: You have a button and an empty paragraph in your HTML2. Find the elements in JavaScript
Done when: You have two variables referring to the button and the paragraph3. Listen for a click event
Done when: The console prints a message every time you click the button4. Update the page content dynamically
Done when: Clicking the button makes the text "Welcome to JavaScript!" appear on the page5. Track state with a counter variable
Done when: The message updates with an accurate, incrementing click count6. Change CSS styles with JavaScript
Done when: After 5 clicks, the button permanently turns red
Finished every step?
Reflect
- Why did we use `let` for the counter variable instead of `const`?
- What is the difference between `console.log()` and modifying `textContent`?
- Why must the counter variable be declared outside the event listener function?

