How to Create a Drag & Drop Sortable List with jQuery UI

Enhance your web applications with an interactive drag-and-drop sortable list using jQuery UI! This feature improves user experience by allowing users to reorder list items effortlessly. In this tutorial, we will walk through creating a sortable list with HTML, CSS, and JavaScript.


What You’ll Learn

  • How to structure a sortable list with HTML
  • Styling the list for a clean UI using CSS
  • Enabling drag-and-drop functionality with jQuery UI
  • Maintaining the updated order dynamically

Step 1: HTML Structure

The first step is to create a basic unordered list (<ul>) containing list items (<li>). Each item represents an element that users can reorder.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Drag & Drop Order List</title>
    <link rel="stylesheet" href="styles.css">
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
    <h2>Drag & Drop to Reorder</h2>
    <ul id="sortable">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
    </ul>
    <script src="script.js"></script>
</body>
</html>

Step 2: CSS Styling

To improve the visual appeal, we add some styling. This ensures a clean and modern design while maintaining an easy-to-use interface.

body {
    font-family: Arial, sans-serif;
    padding: 20px;
}

ul {
    list-style: none;
    padding: 0;
    width: 300px;
}

li {
    padding: 10px;
    margin: 5px 0;
    background: #f4f4f4;
    border: 1px solid #ddd;
    cursor: grab;
    border-radius: 5px;
}

Step 3: JavaScript for Drag & Drop

Using jQuery UI, we enable drag-and-drop functionality for list items. This allows users to rearrange the items by simply dragging them.

$(function() {
    $("#sortable").sortable();
    $("#sortable").disableSelection();
});

Conclusion

With just a few lines of HTML, CSS, and JavaScript, you’ve successfully created a fully functional drag-and-drop sortable list. This feature is highly useful for dashboards, to-do lists, and many other interactive applications.

Next Steps: Try integrating this functionality into a larger project, such as a task management app or a dynamic list-based UI.

📌 Brought to you by Madras Academy – Subscribe for more coding tutorials!

💻 Source Code: GitHub Repository

📺 Watch the Video Tutorial:

📢 Don’t forget to LIKE 👍, SHARE 🔄, and SUBSCRIBE 🔔 to stay updated!

#MadrasAcademy #SortableList #jQueryUI #DragAndDrop #JavaScript #WebDevelopment #UIUX #CodingTutorial

Leave a Reply

Your email address will not be published. Required fields are marked *