Open In App

jQuery UI Sortable connectWith Option

Improve
Improve
Like Article
Like
Save
Share
Report

jQuery UI is a web-based technology and consists of GUI widgets, visual effects, and themes implemented using the jQuery, JavaScript Library. jQuery UI is the best tool for building UI interfaces for the webpages. It can also be used to build highly interactive web applications or can be used to add widgets easily.

In this article, we will learn how to use the jQuery UI Sortable connectWith Option. Using this option, a selector of other sortable elements that the items from this list should be connected to. To be connected in both directions, the connectWith option must be set on both sortable elements. The default of this option is “false”.

Syntax:

The connectWith option takes a Selector type of value and the syntax is as follows.

$( ".selector" ).sortable({
  connectWith: "#shopping-cart"
});
  • Get the connectWith option

    var connectWith = $( ".selector" )
        .sortable( "option", "connectWith" );
  • Set the connectWith option

    $( ".selector" ).sortable( "option", 
        "connectWith", "#shopping-cart" );

CDN Link: The following jQuery Mobile scripts will be needed for your project so we need to add these scripts to your project.

<link rel=”stylesheet” href=”https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css”>
<script src= “https://code.jquery.com/jquery-1.12.4.js”></script>
<script src= “https://code.jquery.com/ui/1.12.1/jquery-ui.js”></script>

Example: This example describes the uses of the jQuery UI Sortable connectWith Option.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <link rel="stylesheet" 
          href=
    <script src=
    </script>
    <script src=
    </script>
  
    <style>
        .list {
            background-color: green;
            border: 1px solid gray;
        }
          
        .items {
            list-style-type: none;
            margin-left: 5px;
            padding: 0;
            width: 200px;
            float: left;
        }
          
        .items li {
            margin: 5px;
            padding: 5px;
            cursor: pointer;
            border-radius: 5px;
            font-size: 20px;
        }
          
        .o {
            background-color: #00ff44;
        }
    </style>
    <script>
        $(function () {
            $("#sortable1,#sortable3").sortable({
                connectWith: "#sortable3",
                dropOnEmpty: true
            });
          
            $("#sortable2").sortable({
                connectWith: "#sortable3",
                dropOnEmpty: false
            });
          
            $("#sortable1, #sortable2, #sortable3").disableSelection();
        });
    </script>
</head>
  
<body>
    <center>
        <h1 style="color:green;">
            GeeksforGeeks
        </h1>
  
        <h4>jQuery UI Sortable connectWith Option</h4>
          
        <ul id="sortable1" class="items">
            <li class="list">Drop on empty</li>
            <li class="list">Drop on empty</li>
        </ul>
          
        <ul id="sortable2" class="items">
            <li class="list o">No drop on empty</li>
            <li class="list o">No drop on empty</li>
        </ul>
          
        <ul id="sortable3" class="items"
            style="height:200px;background-color:#a0ff94">
        </ul>
    </center>
</body>
  
</html>


Output:

jQuery UI Sortable connectWith Option

jQuery UI Sortable connectWith Option

Reference: https://api.jqueryui.com/sortable/#option-connectWith



Last Updated : 29 Jan, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads