HTML5 UL LI Draggable

Anand Chowdhary picture Anand Chowdhary · Sep 8, 2012 · Viewed 54.9k times · Source

I have an unordered list:

  • List item 1
  • List item 2
  • List item 3
  • List item 4
  • List item 5

Implemented with this code:

<ul>
  <li>List item 1</li>
  <li>List item 2</li>
  <li>List item 3</li>
  <li>List item 4</li>
  <li>List item 5</li>
</ul>

Now, I want it to be draggable. For example, if I drag "List item 5" upward, I can place it between "List item 2" and "List item 3", and it'll become third.

I want to do this without jQuery, just plain Javascript. Also, I'd like to use native HTML5 draggable="true". Any help would be appreciated.

Answer

18bytes picture 18bytes · Sep 8, 2012

Attribute "draggable" only enables the element for dragging. You need to implement the DnD listener and implement the drop event to make the changes you want.

You will find the same problem you want to solve in this example: http://www.html5rocks.com/en/tutorials/dnd/basics/

In the example they implement drag-drop for columns A, B and C. User can change the order by DnD.