👀 Live Preview
Drag the box into the zone. drop calls getData and updates status:
Waiting for drop…

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.
Inline JS.
On target.
Read payload.
dragover + drop.
Preferred way.
dragend/dragover.
ondrop AttributeThe 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.
Without event.preventDefault() in dragover on the drop target, the browser blocks the drop and ondrop never runs. Always pair drop with dragover.
Set ondrop on a drop target (with ondragover) or assign element.ondrop:
<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>drop fires on the drop target, not on the draggable source.event.preventDefault() in dragover on the same target so drop is allowed.event.preventDefault() in the drop handler to prevent the browser default action.event.dataTransfer.getData(type) (set in dragstart on the source).event.dataTransfer.files instead of getData.element.ondrop = function(event) { … }.element.addEventListener("drop", handler).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.zone.ondrop = (e) => { e.preventDefault(); … } assigns the handler.ondragover="event.preventDefault()" or an equivalent dragover handler.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"));
};| Event | When it fires | Handler |
|---|---|---|
dragenter | Once when pointer enters target | ondragenter |
dragover | Repeatedly while over target | ondragover + preventDefault |
dragleave | When pointer leaves target | ondragleave |
drop | Once when released on valid target | ondrop |
dragend | Once when drag ends (source) | ondragend |
getData | Read in drop after setData in dragstart | event.dataTransfer.getData(type) |
files | OS file drops | event.dataTransfer.files |
| Target | Supported? | Notes |
|---|---|---|
<div>, <section> | Yes | Common drop zones; pair dragover + drop |
<ul>, <ol> | Yes | Sortable lists; getData with item id on drop |
<textarea>, <input> | Yes | Accept text or file drops with preventDefault |
<canvas> | Yes | Custom drop targets for images or files |
| Draggable source | Separate | Use ondragstart on sources, not ondrop |
ondrop vs ondragend vs ondragenter / ondragover / ondragleave| Handler | When it fires | Element | Typical use |
|---|---|---|---|
ondragenter | Once when entering target | Drop target | Highlight zone, show drop hint |
ondragover | Repeatedly while over target | Drop target | preventDefault to allow drop; set dropEffect |
ondragleave | When leaving target bounds | Drop target | Remove highlight, reset hover state |
ondrop | Once when released on valid target | Drop target | getData, move DOM node, accept files |
ondragend | Once when drag ends | Draggable source | Reset source styles; fires even if drop failed |
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.
Drag the box into the zone. drop calls getData and updates status:
Waiting for drop…
Pair ondragover with preventDefault and read getData in ondrop:
<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>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.
Assign the handler with element.ondrop or addEventListener:
<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>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.
Full mini workflow: drop reads getData on the target; dragend always fires on the source:
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;
});drop finalizes the transfer on the receiver. dragend on the source runs afterward to reset styles or log the outcome via dropEffect.
aria-live regions when drop updates status text so screen reader users know the transfer succeeded.dragenter and dragover fire on the drop zone while hovering.
Call preventDefault in dragover so the browser permits drop.
User releases; call preventDefault and getData on target.
dragend fires on the draggable source; dropEffect reports outcome.
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.
ondrop supportAll major browsers fire the underlying event and honor the ondrop handler attribute.
Bottom line: Universal support for drop on targets. Always call preventDefault in dragover and drop, and read getData or files in the drop handler.
ondrop.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.
ondropBookmark these before wiring your next event handler.
drop on receiver.
EventFires one time.
BehaviorRead in drop.
Use casePreferred.
ModernpreventDefault.
Comparedrop event fires on a drop target—once when the user releases a dragged item over a valid receiver.event.preventDefault() in dragover on the drop target; without it, ondrop never fires.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.element.addEventListener("drop", …). Inline ondrop works for simple demos.event.dataTransfer.files in the drop handler. Still call preventDefault in dragover and drop.Practice inline ondrop, addEventListener, dragover preventDefault, and the full drop workflow in the Try It editor.
5 people found this page helpful