← All articles

JavaScript

Small JavaScript interactions

Add behavior without a large framework.

On this page

  1. Progressive enhancement
  2. Use event listeners
  3. Treat text as text

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";
});

Related reading