0Pricing
JavaScript Academy · Lesson

Making Elements Draggable

Enable dragging and handle drag events.

Making Elements Draggable is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The HTML Drag and Drop API

The native Drag and Drop API lets users grab elements and drop them elsewhere. It is event-driven: source elements fire drag events, and drop targets fire their own. No library required.

The draggable Attribute

By default only links and images are draggable. Add draggable="true" to make any element draggable.

<div draggable="true" id="card">Drag me</div>

Setting draggable in JS

You can toggle the behavior from script via the draggable property.

const card = document.getElementById("card");
card.draggable = true;

The dragstart Event

dragstart fires the moment a drag begins. This is where you record what is being dragged.

card.addEventListener("dragstart", (e) => {
  console.log("Started dragging", card.id);
});

Carrying Data with dataTransfer

The event exposes dataTransfer. Use setData(format, value) in dragstart to attach payload that travels with the drag.

card.addEventListener("dragstart", (e) => {
  e.dataTransfer.setData("text/plain", card.id);
});

The drag Event

drag fires continuously while the element moves. Use it sparingly — it can fire many times per second.

card.addEventListener("drag", (e) => {
  // fires repeatedly during the drag
});

The dragend Event

dragend fires when the user releases, regardless of whether the drop succeeded. Use it to reset any source styling.

card.addEventListener("dragend", (e) => {
  card.classList.remove("dragging");
});

Marking the Source Element

A common pattern: add a class on dragstart and remove it on dragend to dim or highlight the dragged item.

card.addEventListener("dragstart", () => card.classList.add("dragging"));
card.addEventListener("dragend", () => card.classList.remove("dragging"));

Allowed Effects

Set effectAllowed in dragstart to declare what operations are valid: "copy", "move", "link", or combinations.

card.addEventListener("dragstart", (e) => {
  e.dataTransfer.effectAllowed = "move";
});

Touch Devices Caveat

The native API has limited touch support on mobile. For touch-first apps consider Pointer Events or a dedicated library; the native API shines on desktop.

Disabling Text Selection

Dragging can accidentally select text. A small CSS rule keeps drags clean.

/* CSS */
.draggable { user-select: none; }

Quick Check

Test draggable basics.

Recap: Making Elements Draggable

You enabled dragging with draggable="true", handled dragstart/drag/dragend, attached payload via dataTransfer.setData, set effectAllowed, and styled the source. Next: drop targets.

Frequently asked questions

Is the “Making Elements Draggable” lesson free?

Yes — the full text of “Making Elements Draggable” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Making Elements Draggable”?

Enable dragging and handle drag events. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Making Elements Draggable” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Making Elements Draggable
  2. Drop Targets and dataTransfer
  3. Visual Feedback During Drag
  4. Dragging Files into the Page
← Back to JavaScript Academy