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.
Syntax
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).
Values
Attribute Values
The attribute value is always a script. Common patterns:
Each activation passes a Pointer/Mouse event object.
event.detail is the consecutive click count (1, 2, 3…).
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>
Button starts closed — panel has hidden, aria-expanded="false".
Click toggleshidden on the panel.
Update aria-expanded so assistive tech knows open/closed state.
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.
Compatibility
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 Chrome1+
Full support
Mozilla Firefox1+
Full support
Apple Safari1+
Full support
Microsoft Edge12+
Full support
OperaAll
Full support
Samsung InternetModern · Full
Full support
Internet ExplorerAll
Supported
onclickWidely supported
Bottom line: Safe to use for click actions everywhere. Prefer buttons, and use addEventListener in production applications.
Remember
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).