Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

DataTransfer: drag data and allowed operations

DataTransfer carries data during dragging. Learn how effectAllowed restricts operations at the source, setData() stores the payload, and dropEffect selects the operation at the target, without performing the copy or move itself.

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

event.dataTransfer.setData(format, data);
event.dataTransfer.effectAllowed = "copyMove";
event.dataTransfer.dropEffect = "copy";

DataTransfer is a Web API interface, not part of ECMAScript itself. During dragging, it is available as event.dataTransfer. Other interfaces, such as clipboard APIs, also use it, but drag-operation properties do not have the same role there. Having Node 24 does not establish browser support for this API; the example requires a browser supporting native HTML Drag and Drop.

DataTransfer.setData(format, data) stores text under a specified type, such as text/plain, and returns undefined. Under the HTML Standard, writing data for the same type again removes the old item and appends its replacement to the end of the list, which can change the order of types. During a real drag, write in dragstart; read with getData() in drop.

DataTransfer.effectAllowed specifies the operations permitted by the source and should be set in dragstart. Its values are none, copy, copyLink, copyMove, link, linkMove, move, all, and the default uninitialized, which permits all operations. The target selects a compatible operation through DataTransfer.dropEffect: none, copy, link, or move. Invalid values for either property are ignored.

In dragover, call preventDefault() to enable dropping, and set dropEffect on every event. It affects feedback, usually the cursor, but does not itself copy or delete data. The application implements that behavior: the example reads text in drop and displays it through textContent, without interpreting it as HTML. A move effect would also require handling removal of the source data.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>DataTransfer copy</title>
<p id="source" draggable="true">Drag this text</p>
<div id="target" style="border: 2px dashed; padding: 2rem">Drop here</div>
<pre id="output" aria-live="polite"></pre>
<script>
  const source = document.getElementById("source");
  const target = document.getElementById("target");
  const output = document.getElementById("output");
  let localDrag = false;

  source.addEventListener("dragstart", (event) => {
    localDrag = true;
    event.dataTransfer.setData("text/plain", "CKTIS");
    event.dataTransfer.effectAllowed = "copy";
  });

  source.addEventListener("dragend", () => {
    localDrag = false;
  });

  target.addEventListener("dragover", (event) => {
    if (!localDrag) return;
    event.preventDefault();
    event.dataTransfer.dropEffect = "copy";
  });

  target.addEventListener("drop", (event) => {
    if (!localDrag) return;
    event.preventDefault();
    const text = event.dataTransfer.getData("text/plain");
    target.textContent = text;
    output.textContent = `Copied: ${text}`;
  });
</script>
</html>

Result

Copied: CKTIS

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