JavaScript: click

The click event runs a handler when a button, link, or other activatable element is clicked, tapped, or activated from the keyboard.

What you will learn

Minimal example

<button id="toggle" type="button" aria-expanded="false">
  Show details
</button>
<p id="details" hidden>Details are visible.</p>
<script>
const button = document.querySelector("#toggle");
const details = document.querySelector("#details");
button.addEventListener("click", () => {
  const open = details.hidden;
  details.hidden = !open;
  button.setAttribute("aria-expanded", String(open));
});
</script>

Use a real <button> for an action and a real <a> for navigation. These elements already provide expected keyboard behavior; adding a click handler does not replace accessible HTML.

Common mistakes