Course offer · HLD · LLD · System Design at ₹799 until 30 Nov (not interview packs)

Drag and Drop List in JavaScript

Reorder subjects in JavaScript with drag events and array.splice. Move up buttons use the same logic; external dropped text is ignored and refresh resets order.

IntermediateJavaScript Mini ProjectsExample 15 of 20

Save this as index.html and open it in a browser.

drag-and-drop-list.js
1<!doctype html>
2<html lang="en">
3<meta charset="utf-8">
4<title>JavaScript practice</title>
5<body>
6<p>Drag a subject onto another, or use its Move up button.</p><ul id="subjects"></ul><p id="status"></p>
7<script>
8const subjects = ['Math', 'History', 'Physics'];
9let dragged = null;
10const list = document.querySelector('#subjects');
11function move(from, to) {
12 if (!Number.isInteger(from) || from < 0 || from >= subjects.length || to < 0 || to >= subjects.length || from === to) return;
13 const [item] = subjects.splice(from, 1); subjects.splice(to, 0, item);
14 render(); document.querySelector('#status').textContent = 'Order: ' + subjects.join(', ');
15}
16function render() {
17 list.replaceChildren();
18 subjects.forEach((subject, index) => {
19 const row = document.createElement('li'); row.draggable = true; row.tabIndex = -1; row.textContent = subject + ' ';
20 row.ondragstart = event => { dragged = index; event.dataTransfer.setData('text/plain', subject); };
21 row.ondragover = event => event.preventDefault();
22 row.ondrop = event => { event.preventDefault(); if (dragged !== null) move(dragged, index); dragged = null; };
23 row.ondragend = () => { dragged = null; };
24 const up = document.createElement('button'); up.textContent = 'Move up'; up.disabled = index === 0;
25 up.onclick = () => { move(index, index - 1); list.children[index - 1].focus(); };
26 row.append(up); list.append(row);
27 });
28}
29render();
30</script>
31</body>
32</html>

Sample Output

Order: Math, Physics, History

Moving Physics up removes index 2 and inserts it at final index 1.

In this one

  • Reorder an array with splice
  • Enable a browser drop target
  • Clear drag state after a gesture
  • Provide a keyboard-friendly alternative

How it works

  1. 1

    Render each subject as a draggable list item.

  2. 2

    Remember the starting index during dragstart.

  3. 3

    Allow a drop by cancelling dragover default behavior.

  4. 4

    Move the source item to the target final index and rebuild the list.

  5. 5

    Move Physics up to see Math, Physics, History in the status.

Edge cases

Dropped external text

A page can receive a drag from another app.

Ignore it unless this list has its own remembered source index. Do not trust external text as an array index.

Touch or keyboard use

Native HTML dragging is not equally convenient everywhere.

The Move up buttons let people reorder without dragging. A larger app should test touch and assistive input carefully.

What's going on

A drag and drop list lets a person change the order of items by moving them. This revision-subject list also includes Move up buttons, so ordering does not depend only on a mouse drag or a browser's touch support.

The subjects array is the saved order for this page. dragstart remembers the source index. dragover calls preventDefault because the browser normally does not allow dropping on arbitrary elements. drop calls move with the remembered source and the target row index.

move removes one item with splice and inserts it at the requested final position. render rebuilds the rows from the changed array. The same move function powers the ordinary buttons, so both controls use the same rules. External dropped text is not inserted: this page only reorders its own items. Refreshing returns to the original order because this example does not store data permanently.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

Which array method removes and inserts here?

Pick an answer to check it.

Question 2

What is the no-drag alternative?

Pick an answer to check it.

Questions people ask

Why call preventDefault during dragover?

It allows that element to act as a drop target in normal HTML drag and drop behavior.

Does dragging change only the picture?

No. move changes the subjects array, then renders the new order.

Will the order survive refresh?

No. Add storage deliberately if you want that behavior.