CKTIS / JavaScript
HTML dragstart, dragover, drop, and dragend events
Separate source and target events so you can set drag data, accept a drop, read the payload, and clean up even when a drag is canceled.
Version: ECMAScript 2024 and the current web platform; runtime support varies
Syntax
source.draggable = true;
source.addEventListener("dragstart", onDragStart);
target.addEventListener("dragover", onDragOver);
target.addEventListener("drop", onDrop);
source.addEventListener("dragend", onDragEnd);The HTML dragstart/dragover/drop/dragend events belong to the browser’s HTML Drag and Drop API, not ECMAScript. dragstart fires at the source, such as an element with draggable="true". In its synchronous handler, you can read the drag data and write it with event.dataTransfer.setData(type, text). The source sets permitted operations through effectAllowed. Calling preventDefault() on dragstart stops the drag from starting.
dragover fires repeatedly over a potential target. When the target accepts the drag, call preventDefault() on dragover and optionally set dropEffect to an operation the source permits. In protected mode you can inspect formats through dataTransfer.types, but you cannot read the payload. The example also cancels dragenter to follow the standard’s model; canceling dragover is usually enough for a zone like this in mainstream browsers.
drop fires at the target when the user drops the data at a location that accepts it. For custom handling, call preventDefault() to suppress the default action and read the payload synchronously with getData(type). A missing format returns an empty string. Treat dropped content as untrusted: use textContent, not innerHTML, to display plain text.
dragend fires at the source after an initiated drag ends, even if it was canceled or the drop failed. Clear temporary state here; the event alone does not prove success. A dropEffect of "none" means no drop operation was performed. For a file dragged in from the operating system, the page does not receive the source events. Support varies by browser and input method; provide a way to perform the action without dragging.
Examples
(() => {
const panel = document.createElement("section");
panel.style.cssText = "position:fixed;top:16px;left:16px;z-index:1000;padding:16px;background:white;color:black;display:flex;gap:16px";
const source = document.createElement("div");
const target = document.createElement("div");
source.draggable = true;
source.textContent = "Drag this card";
source.style.cssText = "padding:16px;border:1px solid;cursor:grab";
target.textContent = "Drop here";
target.style.cssText = "padding:16px;border:2px dashed;min-width:100px";
panel.append(source, target);
document.body.append(panel);
let active = false;
const accepts = (event) => active && event.dataTransfer?.types.includes("text/plain");
source.addEventListener("dragstart", (event) => {
const transfer = event.dataTransfer;
transfer.setData("text/plain", "Card");
transfer.effectAllowed = "copy";
active = true;
source.style.opacity = "0.5";
console.log("dragstart");
});
target.addEventListener("dragenter", (event) => {
if (accepts(event)) event.preventDefault();
});
target.addEventListener("dragover", (event) => {
if (!accepts(event)) return;
event.preventDefault();
event.dataTransfer.dropEffect = "copy";
});
target.addEventListener("drop", (event) => {
if (!accepts(event)) return;
event.preventDefault();
const text = event.dataTransfer.getData("text/plain");
target.textContent = text;
console.log(`drop: ${text}`);
});
source.addEventListener("dragend", () => {
active = false;
source.style.opacity = "";
console.log("dragend");
});
// Exercise the same handlers once without a pointer gesture.
const transfer = new DataTransfer();
const dispatch = (element, type) => element.dispatchEvent(new DragEvent(type, {
bubbles: true,
cancelable: type !== "dragend",
dataTransfer: transfer,
}));
if (dispatch(source, "dragstart")) {
dispatch(target, "dragenter");
// dispatchEvent returns false when the handler cancels dragover.
if (!dispatch(target, "dragover")) dispatch(target, "drop");
dispatch(source, "dragend");
}
})();Result
dragstart drop: Card dragend
Run the code in a browser console on a blank tab. It dispatches synthetic events to exercise the handlers once without a pointer gesture, and the target displays `Card`. To try native drag and drop, drag “Drag this card” onto the target. Pressing Escape during another drag ends it without `drop`. Repeated `dragover` events are not logged.
Related lessons
- JavaScript · Lesson 34 · Intermediate · Version: 3