HTML ondragend Attribute

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

Introduction

The ondragend attribute is an inline event handler that runs JavaScript when the dragend event fires. That happens once on the source element when the user releases the mouse button and the drag operation ends—after dragstart and any drag events. It fires whether the drop succeeded or was cancelled. It is part of the HTML Drag and Drop API. The element must have draggable="true" (for most elements). Use ondragend to reset styles, read event.dataTransfer.dropEffect (none, copy, move, link), or finalize state. Do not confuse it with ondrop, which fires on the target. For production sites, prefer addEventListener("dragend", …) over inline handlers.

What You’ll Learn

01

Event handler

Inline JS.

02

dragend event

Once at end.

03

dropEffect

none/copy/move.

04

vs ondrop

Source vs target.

05

addEventListener

Preferred way.

06

.ondragend

Property API.

Purpose of ondragend Attribute

The primary purpose of ondragend is to define what happens when a drag operation finishes on the source element. Common uses include resetting opacity or CSS classes applied during the drag, reading event.dataTransfer.dropEffect to know if the drop succeeded, clearing temporary state, or updating a status label. It connects the end of pointer movement to JavaScript cleanup without a full page reload.

Do not confuse ondragend with ondrag or ondrop. dragstart fires once when dragging begins; drag fires many times during movement; dragend fires once when the user releases on the source. drop fires on the drop target only when released over a valid receiver. dragend always runs—even if no drop occurred. Avoid alert() in handlers; update visible status text instead.

💡
Check dropEffect in dragend

After release, event.dataTransfer.dropEffect is none if the drag was cancelled or invalid, or copy, move, or link if a drop succeeded. Production code should use element.addEventListener("dragend", handler).

📝 Syntax

Set ondragend on a draggable element or assign element.ondragend:

ondragend.html
<script>

  function handleDragEnd(event) {

    var effect = event.dataTransfer.dropEffect;

    document.getElementById("status").textContent =

      "Drag ended — dropEffect: " + effect;

    event.target.style.opacity = "1";

  }

</script>



<div draggable="true" ondragend="handleDragEnd(event)">Drag me</div>

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

Syntax Rules

  • Value is JavaScript code executed once when the drag ends on the source element.
  • The element must be draggable — set draggable="true" on most elements.
  • dragend fires on the source being dragged, not on drop targets.
  • Event order: dragstart once, then many drag events, then dragend once on the source.
  • dragend fires whether the drop succeeded or was cancelled.
  • JavaScript: element.ondragend = function(event) { … }.
  • Modern alternative: element.addEventListener("dragend", handler).

💎 Values

The ondragend attribute accepts a string of JavaScript code:

  • ondragend="handleDragEnd(event)" — Call a named function with the event object.
  • ondragend="this.style.opacity='1'" — Inline statement to reset styles on release.
  • JavaScript: box.ondragend = (e) => { … } assigns the handler.
  • Read event.dataTransfer.dropEffectnone, copy, move, or link.
ondragend-js.html
const box = document.getElementById("dragBox");

box.draggable = true;



box.addEventListener("dragend", (event) => {

  const effect = event.dataTransfer.dropEffect;

  document.getElementById("status").textContent =

    "Drag ended — dropEffect: " + effect;

  event.target.style.opacity = "1";

});



document.getElementById("dragBox").ondragend = function (event) {

  console.log("dropEffect:", event.dataTransfer.dropEffect);

};

⚡ Quick Reference

EventWhen it firesHandler
dragendOnce when user releases on sourceondragend
dragstartOnce when drag beginsondragstart
dragRepeatedly while source is draggedondrag
dropItem released on targetondrop
dropEffectnone, copy, move, linkRead in dragend handler
Always fires?dragend yes — success or cancelUnlike drop on target

Applicable Elements

TargetSupported?Notes
<div>, <span>YesSet draggable="true"
<img>YesDraggable by default in many browsers
<a href>YesLinks are draggable by default
<li>YesCommon in sortable lists
<button>YesNeeds draggable="true"; add keyboard alternative

ondragend vs ondrag vs ondrop

HandlerWhen it firesElementTypical use
ondragRepeatedly while movingSourceLive feedback during drag
ondragendOnce when drag endsSourceReset styles, read dropEffect, finalize state
ondropOnce when released on valid targetDrop targetAccept payload, move DOM node, update data

Examples Gallery

Inline ondragend showing dropEffect, dynamic element.ondragend assignment, and comparing dragend vs drop with the full dragstart → drag → dragend sequence.

👀 Live Preview

Drag the box onto the drop zone or release elsewhere. dragend shows dropEffect:

Drag this box.
Drop zone

Waiting for dragend…

Example — Inline ondragend on draggable div

Run a function when the user releases a draggable element and show dropEffect:

inline-ondragend.html
<div draggable="true" ondragend="handleDragEnd(event)">Drag me</div>

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



<script>

  function handleDragEnd(event) {

    document.getElementById("msg").textContent =

      "dragend — dropEffect: " + event.dataTransfer.dropEffect;

    event.target.style.opacity = "1";

  }

</script>
Try It Yourself

How It Works

The browser fires dragend on the source element once when the user releases. The inline ondragend attribute calls your cleanup function.

Dynamic Values with JavaScript

Assign the handler with element.ondragend or addEventListener:

dynamic-ondragend.html
<script>

  document.getElementById("dynamicDragElement").ondragend = function (event) {

    document.getElementById("log").textContent =

      "Dynamic dragend — dropEffect: " + event.dataTransfer.dropEffect;

  };



  // Or preferred form:

  document.getElementById("dynamicDragElement").addEventListener("dragend", handler);

</script>
Try It Yourself

How It Works

Register the listener once at page load. When the user releases the drag, your handler runs once on the source element.

Example — dragend vs drop & event sequence

A full drag sequence fires dragstart, many drag events, then dragend on the source. drop fires only on the target:

dragend-vs-drop.html
box.addEventListener("dragstart", () => { log("dragstart"); });

box.addEventListener("drag", () => { log("drag"); });

box.addEventListener("dragend", (e) => {

  log("dragend — dropEffect: " + e.dataTransfer.dropEffect);

});

target.addEventListener("drop", () => { log("drop (target only)"); });
Try It Yourself

How It Works

Put reset logic in ondragend on the source. Put receive logic in ondrop on the target. Both may run in the same gesture when a drop succeeds.

♿ Accessibility

  • Drag is pointer-heavy — Not all users can drag with a mouse. Provide keyboard or button alternatives for the same action.
  • Announce drag end — Use aria-live regions when dragend resets state so screen reader users get feedback.
  • Avoid alert() on dragend — Inline status text or visible UI updates are more accessible than modal alerts.
  • Do not rely on dragend alone — Finalize important actions in accessible controls, not only after a drag gesture.
  • Focus visible — Restore focus and visible outlines after dragend cleanup on interactive elements.

🧠 How ondragend Works

1

User starts drag

dragstart fires once on source.

ondragstart
2

Pointer moves

drag fires repeatedly while moving.

ondrag
3

User releases

dragend fires once on source; read dropEffect.

ondragend
=

Optional drop on target

drop on receiver if valid; dragend still runs.

Browser Support

The dragend event and ondragend handler are part of the HTML Drag and Drop API and supported in all modern browsers — Chrome, Firefox, Safari, and Edge. Touch drag behavior may differ; test on your target devices.

DOM Events · Fully supported

Universal ondragend support

All major browsers fire the underlying event and honor the ondragend 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
ondragend attribute 99% supported

Bottom line: Universal support for dragend on draggable elements. Set draggable="true" and read dropEffect in your dragend handler.

💡 Best Practices

✅ Do

  • Use addEventListener on the element — Clearer than inline ondragend.
  • Set draggable="true" — Required on most elements before drag events fire.
  • Reset UI in dragend — Restore opacity, classes, or ghost state applied during dragstart or drag.
  • Read dropEffect — Distinguish successful drops from cancelled drags with event.dataTransfer.dropEffect.
  • Pair with ondrop on targets — dragend cleans the source; drop handles the receiver.

❌ Don’t

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

Conclusion

The ondragend attribute runs JavaScript when a draggable element’s drag operation ends and the dragend event fires once on the source. Use it for cleanup, reading dropEffect, and finalizing state—whether or not a drop succeeded.

Prefer addEventListener("dragend", …), set draggable="true", pair with ondragstart and ondrop, and offer keyboard alternatives for accessibility.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about ondragend

Bookmark these before wiring your next event handler.

5
Core concepts
🌐 02

Always fires

Success or cancel.

Behavior
🔄 03

dropEffect

none/copy/move/link.

API
📝 04

addEventListener

Preferred.

Modern
05

vs ondrop

Source vs target.

Compare

❓ Frequently Asked Questions

It runs JavaScript when the dragend event fires — once on the source element when the user releases the mouse and the drag operation ends.
Yes. dragend always fires on the source when the user releases. Check event.dataTransfer.dropEffect — it is none when no valid drop occurred.
ondragend fires on the element being dragged (source). ondrop fires on the drop target when the user releases over a valid receiver.
Prefer element.addEventListener("dragend", …). Inline ondragend works for simple demos.
Yes, for most elements. Without draggable="true", the user cannot drag the element and ondragend will not run.
In dragend, it reflects the drop result: none (cancelled), copy, move, or link. Use it to update UI after the drag ends.

Handle drag end with JavaScript

Practice inline ondragend, addEventListener, dropEffect, and dragend vs drop in the Try It editor.

Try ondragend 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