JavaScript
Small JavaScript interactions
Add behavior without a large framework.
On this page
Progressive enhancement
Start with readable HTML. Add JavaScript for convenience rather than requiring it to access basic content.
Use event listeners
Attach handlers with addEventListener instead of mixing inline event attributes into markup. Keep behavior in a dedicated script.
Treat text as text
Assign user-provided strings to textContent when displaying them. Avoid innerHTML for untrusted data.
Example
const button = document.querySelector("button");
button.addEventListener("click", () => {
document.querySelector("#status").textContent = "Ready";
});