👀 Live Preview
Double-click the box to update the message:
Waiting for a double-click…

The ondblclick attribute is an inline event handler that runs JavaScript when the dblclick event fires. That happens when the user double-clicks an element with a mouse or trackpad—two quick clicks in the same spot. Use it for desktop-style actions like opening a file, zooming an image, or renaming an item on double-click. A double-click also fires two click events first, then one dblclick. The event bubbles. For production sites, prefer addEventListener("dblclick", …) over inline handlers. On touch devices, double-click is less intuitive—offer a single-click alternative when possible.
Inline JS.
Double-click.
button, div, img.
Double vs single.
Preferred way.
Property API.
ondblclick AttributeThe primary purpose of ondblclick is to define what happens when a user double-clicks an element. Common uses include opening a document, zooming an image, entering edit mode on a list item, or selecting a word in a custom text area. It connects a deliberate two-click gesture to JavaScript behavior without a full page reload.
Do not confuse ondblclick with onclick. A single click runs click; a double click runs click twice, then dblclick. Use onclick for primary actions and reserve ondblclick for secondary or power-user shortcuts. Avoid alert() in handlers—update visible status text instead.
Production code should use element.addEventListener("dblclick", handler). Inline ondblclick works for tutorials but mixes markup and behavior.
Set ondblclick on any element or assign element.ondblclick:
<script>
function handleDoubleClick() {
document.getElementById("status").textContent = "Double-clicked!";
}
</script>
<button type="button" ondblclick="handleDoubleClick()">Double-click me</button>
<p id="status"></p>button, div, p, img, etc.click events, then one dblclick.element.ondblclick = function() { … }.element.addEventListener("dblclick", handler).dblclick event bubbles — you can delegate on a parent container.The ondblclick attribute accepts a string of JavaScript code:
ondblclick="handleDoubleClick()" — Call a named function.ondblclick="this.classList.toggle('active')" — Inline statement on the element.box.ondblclick = () => { … } assigns the handler.const card = document.getElementById("card");
card.addEventListener("dblclick", () => {
card.classList.toggle("expanded");
});
document.getElementById("card").ondblclick = function () {
console.log("Double-clicked");
};| Event | When it fires | Handler |
|---|---|---|
dblclick | Double-click on element | ondblclick |
click | Single click (fires twice before dblclick) | onclick |
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 |
ondblclick vs onclick vs onmousedown| Handler | When it fires | Typical use |
|---|---|---|
ondblclick | Two clicks in quick succession | Zoom, edit on double-click |
onclick | Full single click (down + up) | Buttons, toggles, actions |
onmousedown | Button pressed down | Drag start, press feedback |
Inline ondblclick on a button, addEventListener dblclick, and comparing click vs double-click on the same element.
Double-click the box to update the message:
Waiting for a double-click…
Run a function when the user double-clicks a button:
<button type="button" ondblclick="handleDoubleClick()">Double-click me</button>
<p id="msg"></p>
<script>
function handleDoubleClick() {
document.getElementById("msg").textContent = "Button double-clicked!";
}
</script>The browser fires dblclick on the button after two quick clicks. The inline ondblclick attribute calls your function at that moment.
Assign the handler with element.ondblclick or addEventListener:
<script>
document.getElementById("dynamicElement").ondblclick = function () {
console.log("Dynamic element double-clicked!");
};
// Or preferred form:
document.getElementById("dynamicElement").addEventListener("dblclick", handler);
</script>Register the listener once at page load. Each double-click runs your handler on the target element.
A double-click fires two click events, then one dblclick:
box.addEventListener("click", () => {
log("click");
});
box.addEventListener("dblclick", () => {
log("dblclick");
});Do not use ondblclick for primary actions users expect on a single click. Reserve it for secondary shortcuts like zoom or rename.
ondblclick alone.<button type="button"> over <div ondblclick> when the action is a control.click event fires once.
Second click in quick succession.
Handler runs on element.
Zoom, rename, open.
The dblclick event and ondblclick handler are supported in all modern browsers — Chrome, Firefox, Safari, and Edge. Double-click timing may vary slightly by browser and OS.
ondblclick supportAll major browsers fire the underlying event and honor the ondblclick handler attribute.
Bottom line: Universal support on all elements. Test double-click timing on desktop; provide single-click alternatives for touch users.
ondblclick.onclick for primary actions; ondblclick for power-user shortcuts.click and dblclick are attached.The ondblclick attribute runs JavaScript when a user double-clicks an element and the dblclick event fires. Use it for desktop-style shortcuts — zoom, rename, open — not for primary actions users expect on a single click.
Prefer addEventListener("dblclick", …), offer single-click alternatives for touch users, and remember that a double-click fires two click events first.
ondblclickBookmark these before wiring your next event handler.
dblclick fires.
Eventbutton, div, img.
ScopeDouble vs single.
PatternPreferred.
ModernThen dblclick.
Notedblclick event fires — when the user double-clicks an element.button, div, p, img, and more.onclick fires on a single click. ondblclick fires on a double click, after two click events.element.addEventListener("dblclick", …). Inline ondblclick works for simple demos.Practice inline ondblclick, addEventListener, and dblclick vs click in the Try It editor.
5 people found this page helpful