Web development · Lesson 04
Make a button do something
Connect a user action to a small, understandable response.
- Find an element with JavaScript
- Respond to a click with an event listener
Helpful background: Basic HTML and a little JavaScript syntax.
Behavior starts with an event
A browser can report events such as clicks, typing, and form submission. JavaScript can listen for an event and run a function in response.
Keep the first interaction small
Find one element, listen for one event, and update one part of the page. Small steps make it easier to see what your code is doing and to debug it.
Explain what changed
A clear interface tells the user that an action worked. Update text, show a message, or change a visible state instead of leaving the result hidden.
const button = document.querySelector('#hello');
const message = document.querySelector('#message');
button.addEventListener('click', () => {
message.textContent = 'Nice work — your event ran.';
});What does addEventListener help your code do?
Your turn
Describe what should happen when a learner clicks a “Show tip” button. Which two page elements would your script need?
The script needs the button and a message element. It listens for a click and updates the message with the tip.
BUILT TOGETHER
Help make this explanation better.
Have a clearer explanation, a useful example, or a correction? Share it with the community.
Be the first to add your name when you improve this lesson.
How to get credited