Element: click event
An element receives a click event when a pointing device button (such as a mouse's primary mouse button) is both pressed and released while the pointer is located inside the element.
| Bubbles | Yes |
|---|---|
| Cancelable | Yes |
| Interface | MouseEvent |
| Event handler property | onclick |
If the button is pressed on one element and the pointer is moved outside the element before the button is released, the event is fired on the most specific ancestor element that contained both elements.
click fires after both the mousedown and mouseup events have fired, in that order.
Usage notes
The MouseEvent object passed into the event handler for click has its detail property set to the number of times the target was clicked. In other words, detail will be 2 for a double-click, 3 for triple-click, and so forth. This counter resets after a short interval without any clicks occurring; the specifics of how long that interval is may vary from browser to browser and across platforms. The interval is also likely to be affected by user preferences; for example, accessibility options may extend this interval to make it easier to perform multiple clicks with adaptive interfaces.
Internet Explorer
Internet Explorer 8 & 9 suffer from a bug where elements with a computed background-color of transparent that are overlaid on top of other element(s) won't receive click events. Any click events will be fired at the underlying element(s) instead. See this live example for a demonstration.
Known workarounds for this bug:
- For IE9 only:
- Set
background-color: rgba(0,0,0,0) - Set
opacity: 0and an explicitbackground-colorother thantransparent
- Set
- For IE8 and IE9: Set
filter: alpha(opacity=0);and an explicitbackground-colorother thantransparent
Safari Mobile
Safari Mobile 7.0+ (and likely earlier versions too) suffers from a bug where click events aren't fired on elements that aren't typically interactive (e.g. <div>) and which also don't have event listeners directly attached to the elements themselves (i.e. event delegation is being used). See this live example for a demonstration. See also Safari's docs on making elements clickable and the definition of "clickable element".
Known workarounds for this bug:
- Set
cursor: pointer;on the element or any of its ancestors. - Add a dummy
onclick="void(0)"attribute to the element or any of its ancestors up to but not including<body>. - Use a typically interactive element (e.g.,
<a>) instead of one that isn't typically interactive (e.g.,<div>). - Stop using
clickevent delegation.
Safari Mobile considers the following elements to be typically interactive (and thus they aren't affected by this bug):
-
<a>(but it must have anhref) -
<area>(but it must have anhref) <button><img><input>-
<label>(but it must be associated with a form control) <textarea>- This list is incomplete; you can help MDN by doing further testing/research and expanding it.
Examples
This example displays the number of consecutive clicks on a <button>.
HTML
<button>Click</button>
JavaScript
const button = document.querySelector('button'); button.addEventListener('click', event => { button.textContent = `Click count: ${event.detail}`; });
Result
Try making rapid, repeated clicks on the button to increase the click count. If you take a break between clicks, the count will reset.
Specifications
| Specification |
|---|
| UI Events # event-type-click |
Browser compatibility
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
click_event |
1 |
12 |
6
Beginning in Firefox 68, Firefox no longer incorrectly sends a
click event for buttons other than the primary mouse button; previouly, there were circumstances in which this would occur. One example: middle-clicking a link would send a click to the document's <html> element. |
9 |
11.6 |
3 |
1 |
18 |
6 |
12.1 |
1 |
1.0 |
on_disabled_form_elements |
Yes
Only works for
<textarea> elements and some <input> element types. |
79
Only works for
<textarea> elements and some <input> element types. |
No |
Yes
Internet Explorer only triggers the
click event on <input> elements of type checkbox or radio when the element is double-clicked. |
Yes
Only works for
<textarea> elements and some <input> element types. |
No |
Yes
Only works for
<textarea> elements and some <input> element types. |
Yes
Only works for
<textarea> elements and some <input> element types. |
No |
Yes
Only works for
<textarea> elements and some <input> element types. |
No |
Yes
Only works for
<textarea> elements and some <input> element types. |
See also
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/Element/click_event