HTML ondragleave Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

The ondragleave attribute is a global event handler that runs JavaScript when the dragleave event fires on a drop target — when a dragged item or text selection leaves that element’s boundary.

Use it to reset visual feedback (remove a highlight class, clear status text). Pair it with ondragenter (highlight on), ondragover (preventDefault() to allow drop), and ondrop. The source needs draggable="true".

Baseline · Widely available.

Not deprecated and not experimental. Stable across modern browsers since about July 2015. Per MDN, dragleave is not cancelable (it may still bubble).

Discouraged as inline HTML.

MDN discourages HTML event handler attributes. Prefer element.addEventListener("dragleave", handler) in production; use inline ondragleave for quick demos and learning.

HTML Syntax

syntax.html
<element ondragleave=" script">

script is JavaScript run when dragleave fires. Prefer: element.addEventListener("dragleave", handler).

Attribute Values

The attribute value is JavaScript. Common patterns:

Value / patternDescriptionTry it
handleLeave(event)Call a named function with the event objectTry it
this.classList.remove('over')Inline statement to clear highlight on the targetTry it
relatedTarget checkIgnore leave when moving into a nested childTry it
addEventListener("dragleave", …)Preferred modern API (supports multiple listeners)Try it

Live Preview

Drag the box into the drop zone, then out again. dragenter highlights; dragleave clears it.

Desktop mouse drag works best. Touch drag support varies by browser.

Drop target
Drag me
Drop zone

Waiting for dragenter / dragleave…

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Clear Highlight on Leave

Pair ondragenter and ondragleave to turn a drop-zone style on and off.

HTML
<div draggable="true">Drag me</div>
<div id="zone" ondragenter="handleEnter(event)" ondragleave="handleLeave(event)">
  Drop zone
</div>
<p id="msg"></p>

<script>
  var zone = document.getElementById("zone");

  zone.addEventListener("dragover", function (e) {
    e.preventDefault();
  });

  function handleEnter(e) {
    zone.classList.add("over");
    document.getElementById("msg").textContent = "dragenter — inside zone.";
  }

  function handleLeave(e) {
    if (!zone.contains(e.relatedTarget)) {
      zone.classList.remove("over");
      document.getElementById("msg").textContent = "dragleave — left zone.";
    }
  }
</script>
Try It Yourself

How It Works

  1. Source uses draggable="true" so a drag can start.
  2. ondragenter adds a highlight when the pointer enters the zone.
  3. ondragleave removes the highlight when the pointer truly leaves.
  4. relatedTarget check avoids clearing when moving onto a child inside the zone.

Example 3 — Enter/Leave Counter Pattern

Count enters and leaves so highlight stays until the depth returns to zero.

HTML
<div draggable="true">Drag me</div>
<div id="zone">
  <p>Outer zone</p>
  <p>Inner text</p>
</div>
<p id="msg"></p>

<script>
  var zone = document.getElementById("zone");
  var depth = 0;

  zone.addEventListener("dragover", function (e) {
    e.preventDefault();
  });

  zone.addEventListener("dragenter", function () {
    depth += 1;
    if (depth === 1) {
      zone.classList.add("over");
      document.getElementById("msg").textContent = "Inside (depth " + depth + ")";
    }
  });

  zone.addEventListener("dragleave", function () {
    depth -= 1;
    if (depth <= 0) {
      depth = 0;
      zone.classList.remove("over");
      document.getElementById("msg").textContent = "Left zone.";
    }
  });
</script>
Try It Yourself

How It Works

  1. Increment depth on every dragenter (parent or child).
  2. Decrement depth on every dragleave.
  3. Highlight only when depth goes from 0 → 1.
  4. Clear only when depth returns to 0 — true exit from the zone.

Edge Cases & Pitfalls

Most flicker bugs come from nested children firing leave too early.

nested

Children re-fire enter/leave

Moving over descendants can clear your highlight mid-hover. Guard with relatedTarget + contains() or a depth counter.

api

Not cancelable

dragleave cannot be cancelled. Do not rely on preventDefault() here — reset styles only.

drop

Leave ≠ block drop

Allowing a drop still needs preventDefault() in dragover. dragleave only reports that the pointer exited.

a11y

Announce state changes

Update an aria-live status when the zone highlights or clears. Avoid alert() on every leave.

touch

Mobile support varies

HTML Drag and Drop is mouse-oriented. Test touch devices; offer a non-drag alternative when needed.

Browser Support

ondragleave is an HTML event handler attribute for the browser Drag and Drop API. It is not a Node.js, Deno, or Bun API.

✓ Baseline · Widely available

ondragleave

The dragleave event is widely available since about July 2015. Desktop Chrome, Firefox, Safari, and Edge support it well. Nested-child leave quirks are the main gotcha, not missing browsers.

96% Global support (approx.)
Google Chrome 4+
Full support
Mozilla Firefox 3.5+
Full support
Apple Safari 3.1+
Full support
Microsoft Edge 12+
Full support
Opera 12+
Full support
Samsung Internet Modern
Full support
Internet Explorer 9+
Supported
ondragleave Widely supported

Bottom line: Safe to use for clearing drop-zone feedback. Prefer addEventListener, guard nested leaves, and keep preventDefault in dragover.

Key Takeaways

  • Role: run JS when a dragged item leaves a drop target — usually to clear highlight.
  • Pair with enter: dragenter on, dragleave off.
  • Nested kids: use relatedTarget or a depth counter to avoid flicker.
  • Not cancelable: reset UI only; enable drop in dragover.

One line: ondragleave clears drop-zone feedback when the drag exits — guard nested children.

Frequently Asked Questions

It is an inline event handler that runs JavaScript when the dragleave event fires on a drop target — when a dragged item or text selection leaves that element’s boundary.
No. Per MDN, dragleave is not cancelable. You cannot stop the leave with preventDefault(). Use it to reset UI (remove a highlight class).
ondragenter fires when the pointer crosses into the target. ondragleave fires when it crosses out. Pair them to turn highlight on and off.
Entering a child can fire dragleave on the parent even though the pointer is still inside the parent. Check event.relatedTarget with contains(), or use an enter/leave counter.
Prefer element.addEventListener("dragleave", handler). Inline ondragleave works for learning. MDN discourages HTML event handler attributes in production.
Neither. Allowing a drop requires preventDefault() in dragover. dragleave only signals that the pointer left the target.

Next: ondragover

Keep a drop allowed while the pointer hovers — preventDefault() lives here.

ondragover tutorial →

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.

8 people found this page helpful