What is draggable?

draggable is a global attribute that marks an element as draggable with the native HTML Drag and Drop API. Links, images and selected text are draggable by default; for other elements, set draggable="true" and handle the drag events to move data to a drop target.

The event flow

  • On the dragged element: dragstart (set data with e.dataTransfer.setData), drag, dragend.
  • On the drop target: dragenter, dragover, dragleave, drop.
  • Crucial detail: a target accepts drops only if you call e.preventDefault() in dragover.
  • In drop, read the data with e.dataTransfer.getData("text/plain").

DataTransfer

  • Carries typed data (text/plain, text/uri-list, custom types).
  • effectAllowed / dropEffect: copy, move or link cursors.
  • setDragImage() customizes the ghost image.
  • Dropped files arrive in e.dataTransfer.files, which powers file-upload drop zones.

Limitations

  • Does not work on touch screens in most mobile browsers.
  • Not keyboard accessible. You must provide an alternative (move up/down buttons, or a select menu).
  • Styling and animation during the drag are limited.

That is why apps often use libraries like dnd-kit or Pragmatic drag and drop, which use pointer events and add keyboard and screen reader support.

Interview tip

Mention the dragover + preventDefault requirement and the accessibility alternative; both are common follow-ups.

Code example

html
1<style>
2 body { font-family: system-ui; display: flex; gap: 16px; }
3 .col { width: 150px; min-height: 120px; border: 2px dashed #0b1020; border-radius: 8px; padding: 8px; }
4 .col.over { background: #fef9c3; }
5 .card { background: #2b4bff; color: white; padding: 6px; border-radius: 6px; margin: 4px 0; cursor: grab; }
6</style>
7<div class="col" data-col="todo"><b>To do</b>
8 <div class="card" draggable="true" id="c1">Write tests</div>
9 <div class="card" draggable="true" id="c2">Fix CLS bug</div>
10</div>
11<div class="col" data-col="done"><b>Done</b></div>
12<script>
13 document.querySelectorAll(".card").forEach((card) =>
14 card.addEventListener("dragstart", (e) => {
15 e.dataTransfer.setData("text/plain", card.id);
16 e.dataTransfer.effectAllowed = "move";
17 })
18 );
19 document.querySelectorAll(".col").forEach((col) => {
20 col.addEventListener("dragover", (e) => { e.preventDefault(); col.classList.add("over"); }); // allow drop
21 col.addEventListener("dragleave", () => col.classList.remove("over"));
22 col.addEventListener("drop", (e) => {
23 e.preventDefault();
24 col.classList.remove("over");
25 col.append(document.getElementById(e.dataTransfer.getData("text/plain")));
26 });
27 });
28</script>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app