--- layout: demo_layout.njk --- ## Sortable In this example we show how to integrate the [Sortable](https://sortablejs.github.io/Sortable/) javascript library with htmx. To begin we intialize the `.sortable` class with the `Sortable` javascript library: ```js htmx.onLoad(function(content) { var sortables = content.querySelectorAll(".sortable"); for (var i = 0; i < sortables.length; i++) { var sortable = sortables[i]; new Sortable(sortable, { animation: 150, ghostClass: 'blue-background-class' }); } }) ``` Next, we create a form that has some sortable divs within it, and we trigger an ajax request on the `end` event, fired by Sortable.js: ```html
``` Note that each div has a hidden input inside of it that specifies the item id for that row. When the list is reordered via the Sortable.js drag-and-drop, the `end` event will be fired. htmx will then post the item ids in the new order to `/items`, to be persisted by the server. That's it! {% include demo_ui.html.liquid %}