HTML ondrop Attribute

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

Introduction

The ondrop attribute is an inline event handler that runs JavaScript when the drop event fires on a drop target — the element receiving the dragged item. It fires once when the user releases the pointer over a valid drop zone. It is part of the HTML Drag and Drop API: pair ondrop with ondragover calling event.preventDefault() so the browser allows the drop, then read the payload with event.dataTransfer.getData() (set in dragstart on the source). Call event.preventDefault() in the drop handler too. Drop targets also receive dragenter, dragover, and dragleave while hovering; the source receives dragend when the drag finishes. For production sites, prefer addEventListener("drop", …) over inline handlers.

What You’ll Learn

01

Event handler

Inline JS.

02

drop event

On target.

03

getData

Read payload.

04

preventDefault

dragover + drop.

05

addEventListener

Preferred way.

06

Compare events

dragend/dragover.

Purpose of ondrop Attribute

The primary purpose of ondrop is to define what happens when a user releases a dragged item onto a drop target. Common uses include reading event.dataTransfer.getData() to accept a payload, moving or copying DOM nodes, updating a status label, and handling files dropped from the OS via event.dataTransfer.files. It connects the final moment of a successful drag gesture to JavaScript without a full page reload.

Do not confuse ondrop with ondragstart—that fires on the source when dragging begins. drop fires on the target exactly once when released. Unlike ondragover (repeated while hovering) or ondragend (once on the source when the drag ends), drop is where you read getData and update the receiver. Avoid alert() in handlers; update visible status text instead.

💡
dragover preventDefault is required

Without event.preventDefault() in dragover on the drop target, the browser blocks the drop and ondrop never runs. Always pair drop with dragover.

📝 Syntax

Set ondrop on a drop target (with ondragover) or assign element.ondrop:

ondrop.html
<script>

  function allowDrop(event) {

    event.preventDefault();

  }



  function handleDrop(event) {

    event.preventDefault();

    var payload = event.dataTransfer.getData("text/plain");

    document.getElementById("status").textContent = "drop — getData: " + payload;

  }

</script>



<div ondragover="allowDrop(event)" ondrop="handleDrop(event)">Drop zone</div>

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

Syntax Rules

  • Value is JavaScript code executed once when the user releases a dragged item on the element.
  • drop fires on the drop target, not on the draggable source.
  • Call event.preventDefault() in dragover on the same target so drop is allowed.
  • Call event.preventDefault() in the drop handler to prevent the browser default action.
  • Read payload with event.dataTransfer.getData(type) (set in dragstart on the source).
  • For OS file drops, read event.dataTransfer.files instead of getData.
  • JavaScript: element.ondrop = function(event) { … }.
  • Modern alternative: element.addEventListener("drop", handler).

💎 Values

The ondrop attribute accepts a string of JavaScript code:

  • ondrop="handleDrop(event)" — Call a named function with the event object.
  • ondrop="event.preventDefault(); var d = event.dataTransfer.getData('text/plain')" — Minimal inline getData.
  • JavaScript: zone.ondrop = (e) => { e.preventDefault(); … } assigns the handler.
  • Pair with ondragover="event.preventDefault()" or an equivalent dragover handler.
ondrop-js.html
const zone = document.getElementById("dropZone");



zone.addEventListener("dragover", (event) => {

  event.preventDefault();

});



zone.addEventListener("drop", (event) => {

  event.preventDefault();

  const payload = event.dataTransfer.getData("text/plain");

  document.getElementById("status").textContent =

    "drop — getData returned: " + payload;

});



document.getElementById("dropZone").ondrop = function (event) {

  event.preventDefault();

  console.log("getData:", event.dataTransfer.getData("text/plain"));

};

⚡ Quick Reference

EventWhen it firesHandler
dragenterOnce when pointer enters targetondragenter
dragoverRepeatedly while over targetondragover + preventDefault
dragleaveWhen pointer leaves targetondragleave
dropOnce when released on valid targetondrop
dragendOnce when drag ends (source)ondragend
getDataRead in drop after setData in dragstartevent.dataTransfer.getData(type)
filesOS file dropsevent.dataTransfer.files

Applicable Elements

TargetSupported?Notes
<div>, <section>YesCommon drop zones; pair dragover + drop
<ul>, <ol>YesSortable lists; getData with item id on drop
<textarea>, <input>YesAccept text or file drops with preventDefault
<canvas>YesCustom drop targets for images or files
Draggable sourceSeparateUse ondragstart on sources, not ondrop

ondrop vs ondragend vs ondragenter / ondragover / ondragleave

HandlerWhen it firesElementTypical use
ondragenterOnce when entering targetDrop targetHighlight zone, show drop hint
ondragoverRepeatedly while over targetDrop targetpreventDefault to allow drop; set dropEffect
ondragleaveWhen leaving target boundsDrop targetRemove highlight, reset hover state
ondropOnce when released on valid targetDrop targetgetData, move DOM node, accept files
ondragendOnce when drag endsDraggable sourceReset source styles; fires even if drop failed

Examples Gallery

Inline ondrop with dragover preventDefault and getData, dynamic element.ondrop assignment, and a full mini workflow comparing ondrop on the target with dragend on the source.

👀 Live Preview

Drag the box into the zone. drop calls getData and updates status:

Drag this box.
Drop zone

Waiting for drop…

Example — Inline ondrop on drop zone

Pair ondragover with preventDefault and read getData in ondrop:

inline-ondrop.html
<div draggable="true" ondragstart="handleDragStart(event)">Drag me</div>

<div ondragover="allowDrop(event)" ondrop="handleDrop(event)">Drop zone</div>

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



<script>

  function handleDragStart(event) {

    event.dataTransfer.setData("text/plain", "Hello from dragstart");

  }

  function allowDrop(event) { event.preventDefault(); }

  function handleDrop(event) {

    event.preventDefault();

    document.getElementById("msg").textContent =

      "drop — getData: " + event.dataTransfer.getData("text/plain");

  }

</script>
Try It Yourself

How It Works

The browser fires drop once on the drop target when the user releases over a zone that allowed drops via dragover preventDefault. The inline ondrop attribute reads the payload set during dragstart.

Dynamic Values with JavaScript

Assign the handler with element.ondrop or addEventListener:

dynamic-ondrop.html
<script>

  document.getElementById("dynamicDropZone").ondrop = function (event) {

    event.preventDefault();

    var payload = event.dataTransfer.getData("text/plain");

    document.getElementById("log").textContent =

      "Dynamic drop — getData: " + payload;

  };



  // Or preferred form:

  document.getElementById("dynamicDropZone").addEventListener("drop", handler);

</script>
Try It Yourself

How It Works

Register the listener once at page load. When the user drops an item, your handler runs once and reads the transfer data set during dragstart.

Example — ondrop on target vs dragend on source

Full mini workflow: drop reads getData on the target; dragend always fires on the source:

ondrop-dragend-workflow.html
source.addEventListener("dragstart", function (event) {

  event.dataTransfer.setData("text/plain", "Item from dragstart");

});



source.addEventListener("dragend", function (event) {

  status.textContent = "dragend on SOURCE — dropEffect: " + event.dataTransfer.dropEffect;

});



zone.addEventListener("dragover", function (event) {

  event.preventDefault();

});



zone.addEventListener("drop", function (event) {

  event.preventDefault();

  var payload = event.dataTransfer.getData("text/plain");

  status.textContent = "drop on TARGET — getData: " + payload;

});
Try It Yourself

How It Works

drop finalizes the transfer on the receiver. dragend on the source runs afterward to reset styles or log the outcome via dropEffect.

♿ Accessibility

  • Drag is pointer-heavy — Not all users can drag with a mouse. Provide keyboard or button alternatives for the same action.
  • Announce drop results — Use aria-live regions when drop updates status text so screen reader users know the transfer succeeded.
  • Avoid alert() on drop — Inline status text or visible UI updates are more accessible than modal alerts after a drop.
  • Do not rely on drag alone — Finalize important actions in accessible controls, not only after a drag gesture.
  • Visible focus — Ensure drop zones have sufficient color contrast and clear hover/active states.

🧠 How ondrop Works

1

User drags over target

dragenter and dragover fire on the drop zone while hovering.

ondragover
2

dragover allows drop

Call preventDefault in dragover so the browser permits drop.

preventDefault
3

drop fires once

User releases; call preventDefault and getData on target.

ondrop
=

Source gets dragend

dragend fires on the draggable source; dropEffect reports outcome.

Browser Support

The drop event and ondrop 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 ondrop support

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

Bottom line: Universal support for drop on targets. Always call preventDefault in dragover and drop, and read getData or files in the drop handler.

💡 Best Practices

✅ Do

  • Always preventDefault in dragover — Required so the browser allows drop on the target.
  • Always preventDefault in drop — Prevents the browser from navigating to dropped links or opening files unexpectedly.
  • Use addEventListener on the target — Clearer than inline ondrop.
  • Read getData in drop — Payload must be set with setData in dragstart on the source.
  • Pair with dragenter/dragleave — Highlight drop zones during hover for better UX.

❌ Don’t

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

Conclusion

The ondrop attribute runs JavaScript once when a user releases a dragged item over a valid drop target and the drop event fires on that element. Its most important job is reading event.dataTransfer.getData() or event.dataTransfer.files to accept the transfer.

Prefer addEventListener("drop", …), call preventDefault in both dragover and drop, pair with ondragstart on sources, and offer keyboard alternatives for accessibility.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about ondrop

Bookmark these before wiring your next event handler.

5
Core concepts
🌐 02

Once on release

Fires one time.

Behavior
🔄 03

getData

Read in drop.

Use case
📝 04

addEventListener

Preferred.

Modern
05

dragover pair

preventDefault.

Compare

❓ Frequently Asked Questions

It runs JavaScript when the drop event fires on a drop target—once when the user releases a dragged item over a valid receiver.
Browsers block drop by default. Call event.preventDefault() in dragover on the drop target; without it, ondrop never fires.
Call event.dataTransfer.getData(type) in the drop handler. The payload must have been set with setData in dragstart on the draggable source.
ondrop fires once on the drop target when the item is released on a valid zone. ondragend fires once on the draggable source when the drag ends—whether or not a drop succeeded.
Prefer element.addEventListener("drop", …). Inline ondrop works for simple demos.
Yes. When users drop files from the desktop, read event.dataTransfer.files in the drop handler. Still call preventDefault in dragover and drop.

Accept drops with getData

Practice inline ondrop, addEventListener, dragover preventDefault, and the full drop workflow in the Try It editor.

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