HTML ondragend Attribute

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

Definition and Usage

The ondragend attribute is an event-handler attribute for the element being dragged. It runs JavaScript when the dragend event fires — the drag operation ends (mouse button released, or Escape cancels the drag).

Use it on the source (with draggable="true") to reset styles or state after dragstart. The drop target uses ondrop. Typical order: dragstart → drag… → dragend.

Baseline · Widely available.

Not deprecated and not experimental. The dragend event is stable across modern browsers since about July 2015. It is cancelable and may bubble.

Touch & keyboard caveat.

HTML drag-and-drop is mainly pointer-based. Offer a non-drag way to move items. Prefer addEventListener in apps.

HTML Syntax

syntax.html
<element draggable="true" ondragend=" script">

script is JavaScript that runs when the drag finishes. Equivalent JS: el.ondragend = handler or el.addEventListener("dragend", handler).

Attribute Values

The attribute value is always a script. Common patterns:

Value / patternDescriptionTry it
scriptAny JavaScript run when the drag endsTry it
ondragend="fn()"Call a named function from the attributeTry it
Reset stylesRemove a “dragging” class set in dragstartTry it
dropEffectRead event.dataTransfer.dropEffect for outcome (none, copy, …)Try it
addEventListener("dragend")Preferred modern listener on the drag sourceTry it

Live Preview

Drag the box, then release (or press Escape). Status updates on dragstart and dragend.

Action

Opacity dims while dragging, then resets on dragend.

Draggable source

Drag me, then release

Idle — start a drag

HTML Examples

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

Example 1 — Inline ondragend Status

Update a message when the drag finishes.

HTML
<p id="item" draggable="true" ondragend="onDragEnd()">Drag me</p>
<p id="status">Idle</p>

<script>
  function onDragEnd() {
    document.getElementById("status").textContent = "dragend fired.";
  }
</script>
Try It Yourself

How It Works

  1. draggable="true" lets the paragraph start a drag.
  2. User releases (or cancels with Escape).
  3. dragend fires on the source element.
  4. ondragend runs onDragEnd() and updates status.

Example 2 — Reset Opacity After dragstart

Classic pattern from MDN: dim while dragging, restore on dragend.

HTML
<p id="item" draggable="true">Drag me</p>
<p id="status">Idle</p>

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

  item.addEventListener("dragstart", function () {
    item.style.opacity = "0.5";
    status.textContent = "Dragging…";
  });
  item.addEventListener("dragend", function () {
    item.style.opacity = "";
    status.textContent = "Drag ended — styles reset";
  });
</script>
Try It Yourself

How It Works

  1. On dragstart set a temporary style (here opacity).
  2. User finishes the drag — success or cancel.
  3. On dragend clear the temporary style.
  4. Source always gets dragend — even if nothing was dropped.

Example 3 — Read dropEffect on dragend

See whether the drag ended with a drop or was canceled.

HTML
<p id="item" draggable="true">Drag me</p>
<p id="zone"
   ondragover="event.preventDefault()"
   ondrop="event.preventDefault()">
  Optional drop zone
</p>
<p id="status">dropEffect: —</p>

<script>
  document.getElementById("item").addEventListener("dragend", function (event) {
    document.getElementById("status").textContent =
      "dropEffect: " + event.dataTransfer.dropEffect;
  });
</script>
Try It Yourself

How It Works

  1. Drop zone calls preventDefault on dragover/drop so a drop is allowed.
  2. On dragend read event.dataTransfer.dropEffect.
  3. Often none if canceled or no valid drop; otherwise copy, move, or link.
  4. Exact values vary by browser and how effectAllowed was set in dragstart.

Edge Cases & Pitfalls

Most confusion is mixing up source dragend with target drop, or forgetting cleanup when the drag is canceled.

source

Fires on the drag source

Put ondragend on the draggable element — not on the drop zone. The zone uses ondrop.

cancel

Still fires on Escape / failed drop

Always clean up in dragend. Do not assume a successful drop happened.

vs drop

Not a substitute for ondrop

Moving DOM nodes into a target belongs in the target’s drop handler (with dragover + preventDefault).

setup

Need draggable="true"

Without it, most elements never start a drag — so dragend never runs.

a11y

Offer a non-drag path

Keyboard and many touch users cannot rely on HTML DnD. Provide buttons or another move UI.

Browser Support

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

✓ Baseline · Widely available

ondragend

Widely available since about July 2015 on desktop. Mobile/touch HTML drag-and-drop remains inconsistent — test your targets and provide fallbacks.

94% 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
Partial / check
Internet Explorer 9+
Supported
ondragend Widely supported (desktop)

Bottom line: Safe for desktop drag cleanup. Prefer addEventListener, always reset source styles in dragend, and offer a non-drag alternative.

Key Takeaways

  • Role: run script when the drag on the source finishes.
  • Cleanup: reset opacity/classes set in dragstart.
  • vs drop: drop is on the target; dragend is on the source.
  • Always: fires on cancel too — clean up every time.

One line: ondragend is the source’s “drag finished” hook — use it to tidy up after every drag.

Frequently Asked Questions

It is an event-handler attribute on the element being dragged. When the drag operation ends — usually by releasing the mouse button or pressing Escape — the dragend event fires and the JavaScript in ondragend runs.
On the drag source (the element with draggable). The drop target receives drop (and dragover). Use dragend on the source to clean up styles or state.
ondrop fires on the drop target when a valid drop happens. ondragend fires on the source when the drag finishes, whether the drop succeeded or was canceled.
Yes. Ending the drag with Escape still fires dragend on the source. Check dataTransfer.dropEffect (often "none") if you need to know the outcome.
Both work. For learning, ondragend in HTML is fine. In production code, prefer element.addEventListener("dragend", handler).
No. The dragend event and ondragend handler are part of the HTML Drag and Drop API and are Baseline widely available since about July 2015.

Next: ondragenter

Run JavaScript when a dragged item enters a potential drop target.

ondragenter 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