References

Beginner-friendly references for web development, with live, editable examples.

The HTML onclick event

Event All modern browsers
In short

The HTML onclick attribute runs JavaScript each time an element is clicked, as in <button onclick="save()">. Pressing Enter or Space on a focused button fires it too. From JavaScript, assign element.onclick or call addEventListener('click', ...).

Overview

The HTML onclick attribute runs JavaScript when the user clicks an element. Its value is the code to run, such as save(), and every HTML element accepts it. The event it handles is called click.

A click isn't only a mouse action. Pressing and releasing the primary mouse button counts, and so do a tap and keyboard activation of elements such as buttons. Enter and Space on a focused button fire a click, Enter on a link does too, and the same keys on a div with tabindex fire nothing.

The browser wraps the attribute's code in a function, so the code can use this for the element and event for the click. That event has been a PointerEvent since Chrome 92, Firefox 129 and Safari 18.2. Its pointerType is empty for keyboard clicks.

An element holds one onclick handler at a time. Assigning element.onclick again, or rewriting the attribute, replaces the old handler, while addEventListener adds listeners side by side. That makes addEventListener the safer choice when two scripts react to the same element.

Syntax

<button type="button" onclick="addToCart(42)">Add to cart</button>
<script>
  function addToCart(id) {
    console.log('Added item', id);
  }
</script>

Example

Live example
<button type="button" onclick="count(this)">Clicked 0 times</button>
<p>Click it with the mouse, or press Tab and then Enter or Space.</p>
<script>
let clicks = 0;
function count(button) {
  clicks++;
  button.textContent = 'Clicked ' + clicks + (clicks === 1 ? ' time' : ' times');
}
</script>

Best practices

  • Put onclick on a <button> with type="button". Inside a form, a button without a type submits the form after your onclick code runs.
  • Write the full call with parentheses, as in onclick="addToCart(42)". A bare name such as onclick="addToCart" does nothing when clicked.
  • Declare the functions that onclick calls in a classic script, not a module. A function inside <script type="module"> isn't global, so the onclick code throws a ReferenceError.
  • Pass the element with this, as in onclick="count(this)", rather than searching for the element again inside the function.
  • Expect inline handlers to fail under a strict Content Security Policy. A script-src 'self' policy blocks onclick attributes, so sites with such a policy attach listeners from script files.

Accessibility

Attach onclick to a button or link so every user can activate it. The click event is device-independent, so touch, keyboard, mouse and assistive technology can all fire it. A div or span with onclick gets no keyboard activation, so keyboard users can't trigger it.

Frequently asked questions

What does onclick do in HTML?
The onclick attribute runs JavaScript when someone clicks the element or activates it with the keyboard. It is the inline form of a listener for the click event.
How do I call a JavaScript function with onclick?
Put the whole call, parentheses included, in the onclick value, such as addToCart(42). A bare function name only refers to the function, so nothing runs.
How do I set onclick in JavaScript?
Assign a function to the element's onclick property, as in button.onclick = () => save(). Use addEventListener('click', save) instead when other code may handle the same click.
How do I stop a link from navigating with onclick?
End the onclick code with return false, or call event.preventDefault(), and the link won't follow its href. In a listener added with addEventListener, return false does nothing, so call preventDefault() there.
What is the difference between onclick and addEventListener?
An element's onclick holds one handler, while addEventListener can attach many listeners to the same click event. Assigning onclick a second time replaces the first handler without any warning.
Does onclick work with the keyboard?
Yes, onclick fires when a focused button gets Enter or Space, or a focused link gets Enter. Checkboxes, radio buttons and summary elements also fire a click from the keyboard, but a div or span made focusable with tabindex doesn't.
Can I use onclick on a div?
Yes, onclick works on a div, but keyboard users can't trigger it there. Use a button instead, or give the div role=button, tabindex=0 and key handlers for Enter and Space.