Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

HTML5 Drag-and-Drop API | HTML Course lesson | BraveProgrammer
  1. Courses
  2. /
  3. HTML Course

Lesson 31 of 55

·HTML

HTML5 Drag-and-Drop API

9 min

Course progress56%

What you'll learn

Keep these ideas in mind as you work through the lesson.

5 points
  • Making elements draggable with `draggable='true'`
  • Key drag‑and‑drop events (`dragstart`, `dragover`, `drop`) and their default behaviours
  • Transferring data via `dataTransfer` and MIME types
  • Accessibility enhancements (ARIA `aria-grabbed`, `aria-dropeffect`)
  • Providing a keyboard alternative for drag‑and‑drop interactions

Native Drag‑and‑Drop

HTML5 adds drag‑and‑drop events to any element with the draggable attribute.

<ul id='source'>
  <li draggable='true' id='item1'>Item 1</li>
  <li draggable='true' id='item2'>Item 2</li>
</ul>
<ul id='target'></ul>
<script>
  const source = document.getElementById('source');
  const target = document.getElementById('target');

  source.addEventListener('dragstart', e => {
    e.dataTransfer.setData('text/plain', e.target.id);
  });

  target.addEventListener('dragover', e => e.preventDefault());
  target.addEventListener('drop', e => {
    e.preventDefault();
    const id = e.dataTransfer.getData('text/plain');
    const dragged = document.getElementById(id);
    target.appendChild(dragged);
  });
</script>

Accessibility

  • Add aria-grabbed (true / false) to indicate whether an element is being dragged.
  • Use aria-dropeffect on the drop target to describe the permitted operation (move, copy).
  • Provide a keyboard alternative (e.g., arrow‑key re‑ordering) because many screen‑reader users cannot use drag‑and‑drop.

Common pitfalls

  • Forgetting e.preventDefault() on dragover prevents dropping.
  • Using text/plain as the only MIME type may limit complex data; other types (e.g., text/uri-list) can be used.

Quick checklist for drag‑and‑drop

  1. Elements have draggable='true'? ✅
  2. dragover event prevents default behaviour? ✅
  3. dataTransfer used correctly with a MIME type? ✅
  4. ARIA attributes (aria-grabbed, aria-dropeffect) added? ✅
  5. Keyboard fallback implemented? ✅

Tip: For sortable lists, consider a lightweight library like SortableJS that handles ARIA and keyboard shortcuts out of the box.

Make it stickPractice what you've learned1 quiz · 2 practical tests · Optional activities from HTML CourseBrowse activities
Previous lessonHTML5 Geolocation APIContinue to lesson 32Web Storage – localStorage & sessionStorageBack to HTML Course curriculum