Drag And Drop Reorders List Items
Reorder list items with native drag events: draggable rows, a dragover preventDefault to allow drops, and a drop handler that moves the task in place.
Code
interface Task {
id: string;
label: string;
}
const tasks: Task[] = [
{ id: 't1', label: 'scaffold project' },
{ id: 't2', label: 'write tests' },
{ id: 't3', label: 'wire CI' },
];
const list = document.createElement('ul');
document.body.append(list);
function render(): void {
list.replaceChildren();
for (const task of tasks) {
const row = document.createElement('li');
row.textContent = task.label;
row.draggable = true;
row.dataset.id = task.id;
list.append(row);
}
}
let draggedId: string | null = null;
list.addEventListener('dragstart', (event) => {
const row = event.target as HTMLElement;
draggedId = row.dataset.id ?? null;
event.dataTransfer?.setData('text/plain', draggedId);
if (event.dataTransfer) event.dataTransfer.effectAllowed = 'move';
});
// dragover must preventDefault, otherwise the drop is not allowed at all
list.addEventListener('dragover', (event) => {
event.preventDefault();
});
list.addEventListener('drop', (event) => {
event.preventDefault();
const row = (event.target as HTMLElement).closest('li');
const id = event.dataTransfer?.getData('text/plain') || draggedId;
if (!row || !id) return;
const targetIndex = tasks.findIndex((task) => task.id === row.dataset.id);
const from = tasks.findIndex((task) => task.id === id);
if (from < 0 || targetIndex < 0 || from === targetIndex) return;
const [moved] = tasks.splice(from, 1);
// removing the dragged row shifts every index above it left by one, so a downward
// drag must aim one slot higher to still land on the target row
const to = from < targetIndex ? targetIndex - 1 : targetIndex;
tasks.splice(to, 0, moved);
render();
console.log('reordered to:', tasks.map((task) => task.label).join(' | '));
});
render();
console.log('reorder list items by dragging a row onto another row');How it works
Drag And Drop Reorders List Items keeps everything in the functions row and render.
The file repeats itself only inside for (const task of tasks).
What to watch for
- The snippet leans on
document— browser-only surfaces a bare Node process cannot provide. - The guard
!row || !idshort-circuits the flow with an earlyreturnwhen its condition holds.
Using it
Signature: const row = (event.target as HTMLElement)
Output lines are prefixed (reordered to:), making each step’s effect easy to spot.
Key statements
list.addEventListener('dragstart', (event) => {
const row = event.target as HTMLElement;
draggedId = row.dataset.id ?? null;
event.dataTransfer?.setData('text/plain', draggedId);
if (event.dataTransfer) event.dataTransfer.effectAllowed = 'move';
});
list.addEventListener('dragover', (event) => {
event.preventDefault();
});
const targetIndex = tasks.findIndex((task) => task.id === row.dataset.id);
Interactive Tools for This Topic
DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.SRI Hash GeneratorGenerate Subresource Integrity (SRI) hashes from resource content using SHA-256, SHA-384, or SHA-512, and copy a ready-to-use script tag.
FAQ
- Does Drag And Drop Reorders List Items run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.