Open In App

jQuery UI Sortable dropOnEmpty 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 dropOnEmpty Option. Using this option, items from this sortable can’t be dropped on an empty connect sortable, if is set to false.

Syntax:

The dropOnEmpty option takes a boolean value and the syntax is as follows. If false, we can’t be dropped any item on an empty connect sortable and vice-versa.

$( ".selector" ).sortable({
 dropOnEmpty: false
});
  • Get the dropOnEmpty option

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

    $( ".selector" ).sortable( "option", "dropOnEmpty", false );

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 dropOnEmpty 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 dropOnEmpty 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 dropOnEmpty Option

jQuery UI Sortable dropOnEmpty Option

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



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