HTML ondblclick Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Events & Handlers

Introduction

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.

What You’ll Learn

01

Event handler

Inline JS.

02

dblclick event

Double-click.

03

Many elements

button, div, img.

04

vs onclick

Double vs single.

05

addEventListener

Preferred way.

06

.ondblclick

Property API.

Purpose of ondblclick Attribute

The 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.

💡
Prefer addEventListener on the element

Production code should use element.addEventListener("dblclick", handler). Inline ondblclick works for tutorials but mixes markup and behavior.

📝 Syntax

Set ondblclick on any element or assign element.ondblclick:

ondblclick.html
<script>

  function handleDoubleClick() {

    document.getElementById("status").textContent = "Double-clicked!";

  }

</script>



<button type="button" ondblclick="handleDoubleClick()">Double-click me</button>

<p id="status"></p>

Syntax Rules

  • Value is JavaScript code executed when the element receives a double-click.
  • Works on almost any element: button, div, p, img, etc.
  • A double-click fires two click events, then one dblclick.
  • JavaScript: element.ondblclick = function() { … }.
  • Modern alternative: element.addEventListener("dblclick", handler).
  • The dblclick event bubbles — you can delegate on a parent container.

💎 Values

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.
  • JavaScript: box.ondblclick = () => { … } assigns the handler.
ondblclick-js.html
const card = document.getElementById("card");

card.addEventListener("dblclick", () => {

  card.classList.toggle("expanded");

});

document.getElementById("card").ondblclick = function () {

  console.log("Double-clicked");

};

⚡ Quick Reference

EventWhen it firesHandler
dblclickDouble-click on elementondblclick
clickSingle click (fires twice before dblclick)onclick
mousedownMouse button pressed downonmousedown
Keyboard on buttonEnter / SpaceAlso fires click
Form value commitAfter edit + bluronchange
Bubbles?click yesDelegate on parent

Applicable Elements

TargetSupported?Notes
<button>YesPrimary and recommended use
<a href>YesPrefer href for navigation
<div>, <span>YesNeeds keyboard + ARIA for a11y
<input type="button">YesForm button input
<img>YesImage map or custom UI only

ondblclick vs onclick vs onmousedown

HandlerWhen it firesTypical use
ondblclickTwo clicks in quick successionZoom, edit on double-click
onclickFull single click (down + up)Buttons, toggles, actions
onmousedownButton pressed downDrag start, press feedback

Examples Gallery

Inline ondblclick on a button, addEventListener dblclick, and comparing click vs double-click on the same element.

👀 Live Preview

Double-click the box to update the message:

Double-click this box.

Waiting for a double-click…

Example — Inline ondblclick on button

Run a function when the user double-clicks a button:

inline-ondblclick.html
<button type="button" ondblclick="handleDoubleClick()">Double-click me</button>

<p id="msg"></p>



<script>

  function handleDoubleClick() {

    document.getElementById("msg").textContent = "Button double-clicked!";

  }

</script>
Try It Yourself

How It Works

The browser fires dblclick on the button after two quick clicks. The inline ondblclick attribute calls your function at that moment.

Dynamic Values with JavaScript

Assign the handler with element.ondblclick or addEventListener:

dynamic-ondblclick.html
<script>

  document.getElementById("dynamicElement").ondblclick = function () {

    console.log("Dynamic element double-clicked!");

  };



  // Or preferred form:

  document.getElementById("dynamicElement").addEventListener("dblclick", handler);

</script>
Try It Yourself

How It Works

Register the listener once at page load. Each double-click runs your handler on the target element.

Example — dblclick vs click

A double-click fires two click events, then one dblclick:

dblclick-vs-click.html
box.addEventListener("click", () => {

  log("click");

});



box.addEventListener("dblclick", () => {

  log("dblclick");

});
Try It Yourself

How It Works

Do not use ondblclick for primary actions users expect on a single click. Reserve it for secondary shortcuts like zoom or rename.

♿ Accessibility

  • Double-click is not keyboard-native — There is no standard keyboard equivalent to double-click. Do not hide primary actions behind ondblclick alone.
  • Offer single-click alternatives — On touch devices, double-click is hard to discover. Provide buttons or menus for the same action.
  • Avoid alert() on dblclick — Inline status text or visible UI updates are more accessible than modal alerts.
  • Use semantic elements — Prefer <button type="button"> over <div ondblclick> when the action is a control.
  • Focus visible — Do not remove focus outlines on interactive elements; users need to see which control is active.

🧠 How ondblclick Works

1

First click

click event fires once.

click
2

Second click

Second click in quick succession.

click
3

dblclick event fires

Handler runs on element.

ondblclick
=

Run your action

Zoom, rename, open.

Browser Support

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.

DOM Events · Fully supported

Universal ondblclick support

All major browsers fire the underlying event and honor the ondblclick handler attribute.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer IE 9+ supported
Full support
Opera Fully supported
Full support
ondblclick attribute 99% supported

Bottom line: Universal support on all elements. Test double-click timing on desktop; provide single-click alternatives for touch users.

💡 Best Practices

✅ Do

  • Use addEventListener on the element — Clearer than inline ondblclick.
  • Reserve for secondary actions — Use onclick for primary actions; ondblclick for power-user shortcuts.
  • Provide mobile alternatives — Double-click is a desktop pattern; add explicit buttons for touch users.
  • Keep handlers fast — Long tasks should use async patterns so the UI stays responsive.
  • Expect two click events — Guard single-click handlers if both click and dblclick are attached.

❌ Don’t

  • Do not use alert() — Update visible status text instead of blocking modal dialogs.

Conclusion

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.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about ondblclick

Bookmark these before wiring your next event handler.

5
Core concepts
🌐 02

Many elements

button, div, img.

Scope
🔄 03

vs onclick

Double vs single.

Pattern
📝 04

addEventListener

Preferred.

Modern
05

Two clicks first

Then dblclick.

Note

❓ Frequently Asked Questions

It runs JavaScript when the dblclick event fires — when the user double-clicks an element.
Most elements: button, div, p, img, and more.
onclick fires on a single click. ondblclick fires on a double click, after two click events.
Prefer element.addEventListener("dblclick", …). Inline ondblclick works for simple demos.
Double-click is mainly a desktop pattern. Offer single-click buttons or menus for mobile users.
Yes. One listener on a parent can handle double-clicks from child elements.

Handle double-clicks with JavaScript

Practice inline ondblclick, addEventListener, and dblclick vs click in the Try It editor.

Try ondblclick demo →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

5 people found this page helpful