Coding Lesson
JavaScript: Interaction
Respond to a button
Your goal: Make a button change visible text.
Learn
An event connects a visitor’s action to your JavaScript. The starter already listens for a button click and changes a paragraph. You will trace that connection, replace the response with your own tip, and check that it happens at the right time.
Walkthrough: why the code works
Identify the two elements
The paragraph has id="message" and initially says Ready to learn? The button has id="tip". Each id identifies one element. The script uses those exact names to find the button and the output paragraph. The button’s label is not its id.
Read the listener from the outside in
getElementById("tip") finds the button. addEventListener registers a function for the click event. The instructions inside that function wait until the button is activated. The script is below the HTML it uses, so those elements already exist when it runs.
document.getElementById("tip").addEventListener("click", function () { document.getElementById("message").textContent = "Practise a little every day."; });Change the response, not the initial message
Replace the quoted text after textContent = inside the function. That is the response after a click. The words inside the original p element are the initial message. Keep both different so you can observe the transition after Run Code.
Display text safely and predictably
textContent replaces the paragraph’s text. It does not append another paragraph on every click, and it does not interpret a string as HTML. If you include an apostrophe in a double-quoted string, it is fine; keep the surrounding straight quotation marks balanced.
Test the button as a visitor
Choose Run Code, confirm the initial message, then activate Show a tip. Repeat the click and check that there is still one message. In the saved page, use Tab to focus the native button and Enter or Space to activate it. You can add aria-live="polite" to the output paragraph so assistive technology can announce its update.
Expected results
After Run Code, the paragraph still displays Ready to learn? until the button is activated.
The first activation replaces it with your own tip. Repeated activations do not duplicate it.
The native button can be activated with a keyboard in the saved page.
Common mistakes and how to fix them
The button does nothing: Compare tip and message with the HTML ids, including capitals. Check the browser console for a syntax error.
The custom tip appears before clicking: You may have changed the initial p text instead of the string inside the listener.
The script cannot find an element: Keep the script after the elements for this exercise. If moving JavaScript into a file in head later, learn how defer changes when it runs.
View starter code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My hobby page</title>
<style></style>
</head>
<body>
<h1>My hobby</h1>
<p id="message">Ready to learn?</p>
<button id="tip">Show a tip</button>
<script>
document.getElementById("tip").addEventListener("click", function () {
document.getElementById("message").textContent = "Start with something small.";
});
</script>
</body>
</html>Your challenge
Change the message shown when the button is clicked.
Keep developing your hobby page: Download HTML before leaving the sandbox, then choose Open HTML file when returning to your project. Load saved draft restores only this lesson’s draft in this browser.
Practise in the sandboxShow a hint
Edit the text inside the quotes after textContent =.
Show a sample answer
Try the challenge first. This is one possible answer or a snippet to adapt.
document.getElementById("tip").addEventListener("click", function () {
document.getElementById("message").textContent = "Practise a little every day.";
});Check your work
Nothing changes until you click; then your own tip appears.
Completion is your own check, not an automatic grade.
