HTML onclick Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The onclick attribute is an event-handler attribute. It runs JavaScript when the click event fires — typically a mouse press-and-release on the element, a touch tap, or a keyboard activation (such as Enter or Space on a <button>).

click fires after mousedown and mouseup. Prefer real buttons and links for actions — not clickable <div>s — so keyboard and assistive tech work by default.

Baseline · Widely available.

Not deprecated and not experimental. The click event is stable across modern browsers since about July 2015.

Prefer addEventListener in apps.

Inline onclick is fine for learning. Production code usually uses el.addEventListener("click", handler). Strict Content Security Policy (CSP) may also block inline handlers.

HTML Syntax

syntax.html
<element onclick=" script">

script is JavaScript that runs when click fires. Equivalent JS: el.onclick = handler or el.addEventListener("click", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run on a click / activationTry it
onclick="fn()"Call a named function from the attributeTry it
addEventListener("click")Preferred modern listener; supports multiple handlersTry it
event.detailConsecutive click count (resets after a short pause)Try it
Toggle / UI actionShow, hide, or update content from a button clickTry it

Live Preview

Click the button (or focus it and press Enter / Space). Status shows each activation.

Action

Uses a real <button type="button"> so keyboard activation works.

Button

Clicks: 0

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Inline onclick on a Button

Call a function when the button is activated.

HTML
<button type="button" onclick="sayHello()">Say hello</button>
<p id="msg">Waiting for a click…</p>

<script>
  function sayHello() {
    document.getElementById("msg").textContent = "Hello!";
  }
</script>
Try It Yourself

How It Works

  1. Use <button type="button"> so a click does not submit a form by accident.
  2. User activates with mouse, touch, or keyboard.
  3. click fires and onclick runs sayHello().
  4. Update the page — here, set a message in a paragraph.

Example 2 — addEventListener and Click Count

Preferred script pattern; event.detail shows consecutive clicks.

HTML
<button type="button" id="counter">Click</button>

<script>
  document.getElementById("counter").addEventListener("click", function (event) {
    this.textContent = "Click count: " + event.detail;
  });
</script>
Try It Yourself

How It Works

  1. Select the button and register a click listener.
  2. Each activation passes a Pointer/Mouse event object.
  3. event.detail is the consecutive click count (1, 2, 3…).
  4. After a short pause, the counter resets on the next click.

Example 3 — Toggle a Message

A common UI pattern: show or hide content on click.

HTML
<button type="button" id="toggle" aria-expanded="false" aria-controls="panel">
  Show tip
</button>
<p id="panel" hidden>Use a real button for clickable actions.</p>

<script>
  var btn = document.getElementById("toggle");
  var panel = document.getElementById("panel");
  btn.addEventListener("click", function () {
    var open = panel.hidden;
    panel.hidden = !open;
    btn.setAttribute("aria-expanded", open ? "true" : "false");
    btn.textContent = open ? "Hide tip" : "Show tip";
  });
</script>
Try It Yourself

How It Works

  1. Button starts closed — panel has hidden, aria-expanded="false".
  2. Click toggles hidden on the panel.
  3. Update aria-expanded so assistive tech knows open/closed state.
  4. Change the label (“Show tip” / “Hide tip”) to match the UI.

Edge Cases & Pitfalls

Most problems are accessibility shortcuts — clickable divs — or confusing click with double-click.

a11y

Prefer <button> over a clickable div

Buttons are focusable and activate with Enter/Space. A div with only onclick often fails keyboard users.

forms

Use type="button" in forms

A bare <button> inside a form defaults to submit. Set type="button" for actions that should not submit.

dblclick

Not the same as ondblclick

Single activation → click. Two quick clicks → dblclick (and detail can climb on click too).

disabled

Disabled controls do not click

A disabled button does not fire click for normal user activation.

csp

Inline handlers vs CSP

Sites with a strict Content Security Policy may block onclick="…". External script + addEventListener is safer.

Browser Support

onclick is an HTML event-handler attribute in the browser. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

onclick

Widely available since about July 2015. Click works with mouse, touch, and keyboard activation on buttons across modern browsers.

98% Global support (approx.)
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera All
Full support
Samsung Internet Modern · Full
Full support
Internet Explorer All
Supported
onclick Widely supported

Bottom line: Safe to use for click actions everywhere. Prefer buttons, and use addEventListener in production applications.

Key Takeaways

  • Role: run script when the user activates an element (click / tap / key).
  • Use buttons: real <button> (or links) beat clickable divs.
  • Order: mousedown → mouseup → click.
  • Apps: prefer addEventListener("click", …).

One line: onclick runs on activation — use a real button, and move handlers to script in real apps.

Frequently Asked Questions

It is an event-handler attribute. When the user activates an element with a click (mouse, touch, or keyboard equivalent such as Enter/Space on buttons), the click event fires and the JavaScript in onclick runs.
Almost any HTML element can have onclick, but interactive controls like button are the right choice for actions. Prefer real buttons and links over clickable divs.
Yes for elements with a default key activation, such as button (Enter or Space). A plain div with tabindex may receive focus but does not automatically get click from those keys unless you add keyboard handling.
onclick fires for a single activation. ondblclick fires on a double-click. Rapid clicks also set event.detail to the consecutive click count on the click event.
Both work. For learning, onclick in HTML is fine. In production code, prefer element.addEventListener("click", handler) so you can attach multiple listeners and keep script out of markup.
No. The click event and onclick handler are standard and Baseline widely available since about July 2015. Some sites restrict inline handlers with Content Security Policy (CSP).

Next: oncontextmenu

React when the user opens a context (right-click) menu.

oncontextmenu tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful