👀 Live Preview
Click the button to update the message:
Waiting for a click…

The onclick attribute is an inline event handler that runs JavaScript when the click event fires. That happens when the user clicks an element with a mouse, trackpad, or touch tap—or when they activate a focusable control with the keyboard (Enter or Space on a <button>). It is one of the most common ways to add interactivity: toggling panels, submitting actions, updating counters, or showing messages. The click event bubbles, so you can handle many buttons with one listener on a parent. For production sites, prefer addEventListener("click", …) over inline handlers.
Inline JS.
Mouse / tap.
button, div, a.
Single vs double.
Preferred way.
Property API.
onclick AttributeThe primary purpose of onclick is to define what happens when a user clicks an element. Common uses include running a function when a button is pressed, toggling a menu open or closed, incrementing a counter, copying text to the clipboard, or showing a confirmation message. It connects user pointer actions directly to JavaScript behavior without a full page reload.
Do not confuse onclick with onchange. Click responds to pointer activation; change responds when a form value is committed. For navigation to another page, prefer <a href> over onclick="location.href=…" so links work without JavaScript and are easier for users and search engines.
Production code should use button.addEventListener("click", handler). Inline onclick on <button> works for tutorials but mixes markup and behavior.
Set onclick on any element or assign element.onclick:
<script>
function handleClick() {
document.getElementById("status").textContent = "Button clicked!";
}
</script>
<button type="button" onclick="handleClick()">Click me</button>
<p id="status"></p>button, a, div, input, img, etc.type="button" on buttons inside forms to avoid accidental submit.element.onclick = function() { … }.element.addEventListener("click", handler).click event bubbles — you can delegate on a parent container.The onclick attribute accepts a string of JavaScript code:
onclick="handleClick()" — Call a named function.onclick="this.classList.toggle('active')" — Inline statement on the element.btn.onclick = () => { … } assigns the handler.const btn = document.getElementById("save");
btn.addEventListener("click", () => {
console.log("Save clicked");
});
// Dynamic assignment
document.getElementById("toggle").onclick = function () {
this.classList.toggle("open");
};| Event | When it fires | Handler |
|---|---|---|
click | Primary pointer activation | onclick |
dblclick | Double click | ondblclick |
mousedown | Mouse button pressed down | onmousedown |
| Keyboard on button | Enter / Space | Also fires click |
| Form value commit | After edit + blur | onchange |
| Bubbles? | click yes | Delegate on parent |
| Target | Supported? | Notes |
|---|---|---|
<button> | Yes | Primary and recommended use |
<a href> | Yes | Prefer href for navigation |
<div>, <span> | Yes | Needs keyboard + ARIA for a11y |
<input type="button"> | Yes | Form button input |
<img> | Yes | Image map or custom UI only |
onclick vs ondblclick vs onmousedown| Handler | When it fires | Typical use |
|---|---|---|
onmousedown | Button pressed down | Drag start, press feedback |
onclick | Full click (down + up) | Buttons, toggles, actions |
ondblclick | Two clicks in quick succession | Zoom, edit on double-click |
Inline onclick on a button, addEventListener click, and event delegation on a parent container.
Click the button to update the message:
Waiting for a click…
Run a function when the user clicks a button:
<button type="button" onclick="showMessage()">Click me</button>
<p id="msg"></p>
<script>
function showMessage() {
document.getElementById("msg").textContent = "Button clicked!";
}
</script>The browser fires click on the button when activated. The inline onclick attribute calls your function at that moment.
Attach the handler with addEventListener:
<script>
document.getElementById("save").addEventListener("click", function () {
console.log("Save clicked");
});
// Or property form:
document.getElementById("toggle").onclick = function () { /* … */ };
</script>Register the listener once at page load. Each click runs your handler with this pointing at the element.
Because click bubbles, one listener on a parent can handle many buttons:
document.getElementById("toolbar").addEventListener("click", (e) => {
if (e.target.matches("button[data-action]")) {
console.log("Action:", e.target.dataset.action);
}
});The click event travels up the DOM. The parent checks event.target to see which child was clicked, avoiding a separate handler per button.
<button type="button"> over <div onclick> so keyboard and screen reader users can activate controls.click on Enter and Space. Custom clickable divs need tabindex="0" and key handlers.<a href> for links instead of onclick="location.href=…" so users can open in new tabs and navigate without JS.Mouse, tap, or keyboard activation.
Full press-and-release on target.
Handler runs on element.
Toggle, save, navigate.
The click event and onclick handler are supported in all modern browsers — Chrome, Firefox, Safari, and Edge. Behavior is consistent across elements and input devices.
onclick supportAll major browsers fire the underlying event and honor the onclick handler attribute.
Bottom line: Universal support on all elements. Test with mouse, touch, and keyboard (Tab + Enter/Space on buttons).
onclick.<button type="button"> instead of clickable divs when possible.type="submit" defaults.click bubbles — one listener on a list container can handle many items.href for better UX and SEO.The onclick attribute runs JavaScript when a user clicks an element and the click event fires. Use it to add interactivity — toggles, counters, custom actions — on buttons and other elements.
Prefer addEventListener("click", …), use semantic <button> elements, and remember that click bubbles for event delegation on parent containers.
onclickBookmark these before wiring your next event handler.
click fires.
Eventbutton, div, a.
ScopeSingle vs double.
PatternPreferred.
ModernDelegate clicks.
Noteclick event fires — when the user clicks or activates an element.button, a, div, input, and more. Prefer semantic interactive elements.onclick is one inline handler in HTML. addEventListener("click", …) is the modern JS API with multiple handlers and cleaner separation.onclick fires on a single click. ondblclick fires on a double click.<button type="button">. Divs with onclick need extra keyboard and ARIA support to be accessible.Practice inline onclick, addEventListener, and event delegation in the Try It editor.
5 people found this page helpful