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.
Syntax
HTML Syntax
syntax.html
<element ondragleave="script">
script is JavaScript run when dragleave fires. Prefer: element.addEventListener("dragleave", handler).
Values
Attribute Values
The attribute value is JavaScript. Common patterns:
Increment depth on every dragenter (parent or child).
Decrement depth on every dragleave.
Highlight only when depth goes from 0 → 1.
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.
Compatibility
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 Chrome4+
Full support
Mozilla Firefox3.5+
Full support
Apple Safari3.1+
Full support
Microsoft Edge12+
Full support
Opera12+
Full support
Samsung InternetModern
Full support
Internet Explorer9+
Supported
ondragleaveWidely supported
Bottom line: Safe to use for clearing drop-zone feedback. Prefer addEventListener, guard nested leaves, and keep preventDefault in dragover.
Remember
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.