HTML ondrop Attribute

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

Definition and Usage

The ondrop attribute is a global event handler that runs JavaScript when the drop event fires on a drop target — when a dragged item or text selection is released on that element.

To make drop fire, call event.preventDefault() in ondragover on the same target. In drop, call preventDefault() again to stop the browser’s default (e.g. opening a link). Pair a draggable source with ondragstart (setData) and read it here with getData.

Baseline · Widely available.

Not deprecated and not experimental. Stable since about July 2015. Per MDN, drop is cancelable and may bubble — and you should always allow it via dragover + preventDefault().

Discouraged as inline HTML.

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

HTML Syntax

syntax.html
<element ondrop=" script">

script is JavaScript run when a drop lands. Prefer: element.addEventListener("drop", handler).

Attribute Values

The attribute value is JavaScript. Common patterns:

Value / patternDescriptionTry it
preventDefault()Stop browser default (open link / navigate)Try it
getData(type)Read payload stored in dragstart with setDataTry it
dataTransfer.filesRead files dropped from the OS / desktopTry it
addEventListener("drop", …)Preferred modern APITry it

Live Preview

Drag the box onto the zone and release. dragover allows the drop; drop moves the item into the zone.

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

Source + drop target
Drag me
Drop zone

Waiting for drop…

HTML Examples

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

Example 1 — Move Element into Drop Zone

Allow drop with dragover, then append the source in drop.

HTML
<div id="item" draggable="true">Drag me</div>
<div id="zone" ondragover="allowDrop(event)" ondrop="handleDrop(event)">
  Drop here
</div>

<script>
  var dragged = null;

  document.getElementById("item").addEventListener("dragstart", function (e) {
    dragged = e.target;
  });

  function allowDrop(e) {
    e.preventDefault(); // required so drop fires
  }

  function handleDrop(e) {
    e.preventDefault(); // stop browser default
    if (dragged) e.currentTarget.appendChild(dragged);
  }
</script>
Try It Yourself

How It Works

  1. dragstart remembers which element is being dragged.
  2. dragover + preventDefault marks the zone as a valid drop target.
  3. drop + preventDefault stops the browser default action.
  4. appendChild moves the source node into the zone.

Example 2 — Read Text with getData

Store a string in dragstart; read it in drop.

HTML
<div id="item" draggable="true">Ticket #42</div>
<div id="zone">Drop here</div>
<p id="msg"></p>

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

  item.addEventListener("dragstart", function (e) {
    e.dataTransfer.setData("text/plain", "Ticket #42");
  });

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

  zone.addEventListener("drop", function (e) {
    e.preventDefault();
    var data = e.dataTransfer.getData("text/plain");
    document.getElementById("msg").textContent = "Got: " + data;
  });
</script>
Try It Yourself

How It Works

  1. setData in dragstart stores text/plain.
  2. dragover still needs preventDefault().
  3. getData in drop uses the same MIME type.
  4. Mismatch of types returns an empty string — keep names identical.

Example 3 — Drop Files from the Desktop

OS file drops use dataTransfer.files (no setData needed).

HTML
<div id="zone">Drop files here</div>
<p id="msg"></p>

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

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

  zone.addEventListener("drop", function (e) {
    e.preventDefault();
    var files = e.dataTransfer.files;
    if (!files.length) {
      document.getElementById("msg").textContent = "No files in this drop.";
      return;
    }
    var names = [];
    for (var i = 0; i < files.length; i++) {
      names.push(files[i].name);
    }
    document.getElementById("msg").textContent = "Files: " + names.join(", ");
  });
</script>
Try It Yourself

How It Works

  1. No source element — the OS supplies the drag payload.
  2. dragover + preventDefault still required for the zone.
  3. files is a FileList on dataTransfer.
  4. Validate on the server if you upload — never trust client-only checks.

Edge Cases & Pitfalls

If drop never fires, check dragover first — not your drop handler.

dragover

Must allow drop in dragover

Without preventDefault() on dragover, the browser refuses the drop and drop never runs.

default

Call preventDefault in drop too

Stops navigation or opening a dropped URL/link as a page load.

types

setData / getData must match

Use the same MIME string (e.g. text/plain) on both ends or you get an empty string.

a11y

Offer a non-drag path

Provide buttons or file inputs for the same move/upload action. Announce results with aria-live.

files

Validate uploads server-side

Client file drops are UX only. Check type, size, and content on the server before saving.

Browser Support

ondrop 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

ondrop

Widely available since about July 2015. Desktop Chrome, Firefox, Safari, and Edge support it well. File-drop UX and touch drag still differ by platform.

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
ondrop Widely supported

Bottom line: Safe to use for drop targets on modern sites. Prefer addEventListener, always pair with dragover + preventDefault, and validate file drops on the server.

Key Takeaways

  • Role: runs on the target when a valid drop lands — move nodes or read data.
  • Requires dragover + preventDefault() or drop never fires.
  • getData / files: read text payloads or OS file drops here.
  • Also preventDefault in drop to block browser navigation defaults.

One line: ondrop handles the landing — unlock it with dragover + preventDefault().

Frequently Asked Questions

It is an inline event handler that runs JavaScript when the drop event fires on a drop target — when a dragged item or text selection is released on that element.
Browsers block drops by default. Call event.preventDefault() in the dragover handler on the same target so drop can fire. MDN recommends always doing this.
Yes, usually. It stops the browser’s default action for the drop (for example, navigating when a link or URL is dropped).
Use event.dataTransfer.getData("text/plain") (or another MIME type) matching what you stored with setData in dragstart. For OS files, use event.dataTransfer.files.
Prefer element.addEventListener("drop", handler). Inline ondrop works for learning. MDN discourages HTML event handler attributes in production.
ondrop fires on the drop target when a valid drop happens. ondragend fires on the source when the drag finishes, whether or not a drop succeeded.

Next: ondurationchange

React when an audio or video element’s duration becomes available or changes.

ondurationchange 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