jquery Plugin

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

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.

Related Post
<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

View Comments

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories