HTML Drag and Drop: Draggable Elements
Mark a draggable item
The enumerated draggable attribute controls whether an HTML element can be dragged. Use draggable="true" or draggable="false", not a bare attribute. Links and images can be draggable by default; ordinary text elements need explicit enabling for element dragging. Markup alone does not implement a custom drop operation.
<p draggable="true">Move this item</p><a href="#target" draggable="false">Read target</a><p id="target">Target</p>The paragraph is draggable; the link explicitly disables dragging.
Transfer text during a drag
A dragstart handler writes a string to event.dataTransfer. Cancel dragover with preventDefault() to allow an ordinary custom target to accept a drop. At drop, read the string with getData and write it as textContent. Here event is the browser-supplied object describing the interaction; no external data is requested.
<p id="card" draggable="true">HTML note</p><div id="zone" style="border: 1px solid; padding: 20px">Drop here</div><button type="button" id="copy">Copy note without dragging</button><script> const card = document.getElementById("card"); const zone = document.getElementById("zone"); card.addEventListener("dragstart", event => { event.dataTransfer.setData("text/plain", card.textContent); }); zone.addEventListener("dragover", event => event.preventDefault()); zone.addEventListener("drop", event => { event.preventDefault(); zone.textContent = event.dataTransfer.getData("text/plain"); }); document.getElementById("copy").addEventListener("click", () => { zone.textContent = card.textContent; });</script>Drag the note onto the zone or activate the button to copy the same text.
Provide a keyboard and pointer alternative
Dragging is not available to every user or device. A normal button can perform the same logical operation without dragging, using click or keyboard activation. Announce a changed result when appropriate; role="status" exposes nonurgent updates to assistive technologies. A drag operation is not an accessibility requirement for rearranging items.
<p id="item">Task A</p><button type="button" id="move">Mark complete</button><p id="state" role="status">Pending</p><script> document.getElementById("move").addEventListener("click", () => { document.getElementById("state").textContent = "Task A complete"; });</script>Activate Mark complete with mouse or keyboard to update the status.
Common mistakes and practice
Do not trust dropped data or insert it as raw HTML. Native drag-and-drop behavior on touch devices varies; test real pointer and keyboard workflows. The code examples cover text transfer, not uploading files or persistent rearrangement.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Drag and Drop: Draggable Elements — Knowledge Check
Apply the interaction concepts taught in this lesson.
Which value makes an ordinary HTML element draggable?
When is drag data normally written?
How can a custom target allow a drop?
When should the target read dropped text?
What makes a drag task more accessible?