Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

HTML drag lifecycle events

Learn the seven HTML drag events, how they are divided between source and target, and when DataTransfer is readable or writable. Understand why dragover needs preventDefault() and why dragend does not prove that a drop succeeded.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

source.addEventListener("dragstart", handler);
source.addEventListener("drag", handler);
source.addEventListener("dragend", handler);
target.addEventListener("dragenter", handler);
target.addEventListener("dragover", handler);
target.addEventListener("dragleave", handler);
target.addEventListener("drop", handler);

HTML drag lifecycle events are seven events in the browser's HTML Drag and Drop API, not a feature of a particular ECMAScript edition. The source receives dragstart when dragging begins, repeated drag events during dragging, and dragend when it finishes. Potential targets receive dragenter on entry, repeated dragover events, dragleave on exit, and drop when an item is released onto a valid target. Repetition counts and timing are not fixed.

Make an ordinary element draggable with draggable = true. In dragstart, write text using event.dataTransfer.setData(). Data can be modified only in dragstart, and its contents read in dragstart and drop; inspect available types rather than contents in dragover. In practice, browsers let a custom target accept the dragged data by calling preventDefault() in dragover. The HTML Standard, however, also requires canceling dragenter for an ordinary element to be selected as a custom drop target. In drop, also cancel the default behavior and read the data with getData().

Use dragend for cleanup even when the gesture is canceled, for example with Escape; receiving it does not prove success. effectAllowed defines the source's permitted operations, while dropEffect indicates the selected operation; neither automatically moves DOM elements. Treat external data as untrusted: validate values and display text with textContent.

The example copies only token-1 and provides a button as an alternative to dragging. Test a real gesture in the target browser: a synthetic DragEvent does not reproduce the full browser-managed operation. Dragging a file from the operating system does not fire source-side dragstart or dragend on the page. The availability of Node 24 does not establish support for these browser interfaces.

Examples

const panel = document.createElement("section");
const source = document.createElement("p");
const target = document.createElement("p");
const alternative = document.createElement("button");
const output = document.createElement("p");

source.textContent = "Drag token-1";
source.draggable = true;
target.textContent = "Drop here";
target.style.cssText = "padding: 2rem; border: 2px dashed;";
alternative.type = "button";
alternative.textContent = "Copy token-1 without dragging";
output.setAttribute("role", "status");
output.textContent = "Ready";
panel.append(source, target, alternative, output);
document.body.append(panel);

function accept(value) {
  if (value === "token-1") {
    output.textContent = `Dropped: ${value}`;
  }
}

source.addEventListener("dragstart", (event) => {
  event.dataTransfer.setData("text/plain", "token-1");
  event.dataTransfer.effectAllowed = "copy";
  source.style.opacity = "0.5";
});

target.addEventListener("dragover", (event) => {
  if (event.dataTransfer.types.includes("text/plain")) {
    event.preventDefault();
    event.dataTransfer.dropEffect = "copy";
  }
});

target.addEventListener("drop", (event) => {
  event.preventDefault();
  accept(event.dataTransfer.getData("text/plain"));
});

source.addEventListener("dragend", () => {
  source.style.opacity = "";
});

alternative.addEventListener("click", () => accept("token-1"));

Result

Dropped: token-1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources