JavaScript Academy · Lesson

Making Elements Draggable

Enable dragging and handle drag events.

Lesson 1 of 413 steps

Making Elements Draggable is a free JavaScript Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, and your progress syncs across the web and the CoddyKit app. The JavaScript Academy course includes 4 lessons in total.

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.

Free to start

Learn JavaScript with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
59
Lessons
200

Frequently Asked Questions

Is the “Making Elements Draggable” lesson free?

Yes — any 3 lessons of the JavaScript Academy learning path, including “Making Elements Draggable”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The JavaScript Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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