What you’ll learn
You’ll connect a class selector to a button and use an event listener to update the interface.
Add a meaningful element
Create a button with a label and an output for feedback. Use HTML element types that already support keyboard interaction.
Style the class
Apply padding and a border to .action. A class can be reused if a second button needs the same style.
Attach behavior
Select .action with querySelector, listen for click, and write a result with textContent. Keep the selector consistent with the class in your markup.
Try the code
<button class="action">Check</button>
<output aria-live="polite"></output>
<style>.action { padding: .5rem; border: 1px solid navy; }</style>
<script>
document.querySelector(".action").addEventListener("click", () => {
document.querySelector("output").textContent = "Checked";
});
</script>Open the editor and paste this sample into the HTML tab.
Troubleshooting
If querySelector returns null, check spelling and placement. A script in the document head may run before the button exists. Put a classic script after the markup or load an external script with defer.
Frequently asked questions
Which tab should I use for a full file?
Use Complete document mode for inline style and script elements. In Split tabs, paste CSS and JavaScript without their surrounding tags.