Drag and Drop Element with JQuery UI Sortable

In this tutorial we will learn how to create drag and drop with sorting functionality with help of jQuery UI. That means we are able to move the object from one location container to another location container.

Sometimes we need to drag one object from location to other location so we need to provide drag and drop with jquery.

Example of Drag-and-Drop with jQuery

drag_drop_jquery_ui

Checkout other tutorial of jQuery,

Simple Example to Create Drag and Drop Element with jQuery

Step 1: Include the below files in section of files. These are jQuery and jQuery UI file.

<script src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="http://code.jquery.com/ui/1.9.1/jquery-ui.js"></script>

Step 2: We will Define HTML Structure or HTML view file to show drag and drop elements list.HTML structure of jQuery UI Sortable is very easy, just we define div as a parent with a specific id and keep all items as the list based on ul and li the combination as follows.

<!-- Main component for a primary marketing message or call to action -->

<h1>Test</h1>

<div id="sortable-div" class="col-sm-12 col-xs-12 col-md-9 addSub">
    <div class="padLR10"></div>

    <div id="dragdrop" class="col-sm-4 col-xs-4 col-md-4">
        <div class="well clearfix">
            <div class="header">Container 1</div>

            <div class="dragbleList">
                <ul class="sortable-list">
                    <li class="sortable-item">Item1</li>

                    <li class="sortable-item">Item2</li>

                    <li class="sortable-item">Item3</li>

                    <li class="sortable-item">Item4</li>
                </ul>
            </div>
        </div>
    </div>

    <div id="dragdrop" class="col-sm-4 col-xs-4 col-md-4">
        <div class="well clearfix">
            <div class="header">Container 2</div>

            <ul class="sortable-list">
                <li class="sortable-item">Item51</li>

                <li class="sortable-item">Item52</li>

                <li class="sortable-item">Item53</li>

                <li class="sortable-item">Item54</li>
            </ul>
        </div>
    </div>

    <div id="dragdrop" class="col-sm-4 col-xs-4 col-md-4">
        <div id="my-timesheet" class="well clearfix">
            <div class="header">Container 3</div>

            <ul class="sortable-list">
                <li class="sortable-item">Item6</li>

                <li class="sortable-item">Item7</li>

                <li class="sortable-item">Item8</li>

                <li class="sortable-item">Item9</li>
            </ul>
        </div>
    </div>
</div>

Step 3: Now we will initializing UI Sortable with help of sortable function.

$(document).ready(function(){
  // Example 1.3: Sortable and connectable lists with visual helper
  $('#sortable-div .sortable-list').sortable({
   connectWith: '#sortable-div .sortable-list',
    placeholder: 'placeholder',
 });
});

This code will be called on jquery on document-ready.We have passed sortable-div and .sortable-list for drag and drop elements containers.

Live Demo & Download Source Code

3 thoughts on “Drag and Drop Element with JQuery UI Sortable

Leave a Reply

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