Open In App

jQuery UI Droppable scope Option

Improve
Improve
Like Article
Like
Save
Share
Report

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

In this article, we are going to learn the jQuery UI Droppable scope Option. The scope option is used to group the sets of draggable and droppable items. A draggable and droppable item should have the same scope value.

Syntax: The scope option item takes a string value and is initialized as follows:

$("#dropFirst").droppable({
    scope: "first",
});
  • Get the scope option:

    var scopeOpt = $("#dropFirst").droppable("option", "scope");
  • Set the scope option:

    $("#dropFirst").droppable("option", "scope", "first");

CDN Links: Use the following CDNs for the jQuery UI project.

<link rel=”stylesheet” href=”https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css” />
<script src=”https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js”></script>
<script src=”https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js”></script>

Example: In the following example, we have two draggable and two droppable items. The scope is set to first and second respectively.

HTML




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" 
          content="IE=edge" />
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" 
          href=
    <script src=
    </script>
    <script src=
    </script>
    <style type="text/css">
    .square_draggable {
        width: 50px;
        height: 50px;
        border: 1px solid black;
        margin: 5px;
        text-align: center;
        line-height: 50px;
        background-color: lightblue;
        cursor: pointer;
        border-radius: 10px;
    }
  
    .square_droppable {
        width: 100px;
        height: 100px;
        border: 1px dotted gray;
        margin: 5px;
        text-align: center;
        line-height: 100px;
        background-color: lightgray;
    }
  
    .container {
        background-color: darkgreen;
        width: 500px;
        height:300px;
        margin: auto;
    }
    </style>
</head>
  
<body>
    <div data-role="page" id="gfgpage">
        <h1 style="color: green;">
            GeeksforGeeks
        </h1>
        <div data-role="main" class="ui-content">
            <h3>jQuery UI Droppable scope option</h3>
            <div id="gfg_container" 
                 class="container">
                <div id="dragFirst" 
                     class="square_draggable">
                        Item 1</div>
                <div style="float:left">
                    <div id="dragSecond" 
                         class="square_draggable">
                         Item 2</div>
                </div>
                <div style="float:left;margin-left:50px;">
                    <div id="dropFirst" 
                         class="square_droppable">
                         Scope : first</div>
                </div>
                <div style="float:left;margin-left:50px;">
                    <div id="dropSecond" 
                         class="square_droppable">
                         Scope : second</div>
                </div>
            </div>
        </div>
    </div>
    <script>
    $(function() {
        $("#dragFirst").draggable({
            scope: "first",
            containment: "#gfg_container"
        });
  
        $("#dragSecond").draggable({
            scope: "second",
            containment: "#gfg_container"
        });
  
        $("#dropFirst").droppable({
            scope: "first",
            drop: function(event, ui) {
                $(this).css("background-color", "lightgreen")
            },
            over: function(event, ui) {
                $(this).css("background-color", "green")
            },
            out: function(event, ui) {
                $(this).css("background-color", "lightgray")
            }
        });
  
        $("#dropSecond").droppable({
            scope: "second",
            drop: function(event, ui) {
                $(this).css("background-color", "lightgreen")
            },
            over: function(event, ui) {
                $(this).css("background-color", "green")
            },
            out: function(event, ui) {
                $(this).css("background-color", "lightgray")
            }
        });
  
    });
    </script>
</body>
  
</html>


Output

jQuery UI Droppable scope Option

jQuery UI Droppable scope Option

Reference: https://api.jqueryui.com/droppable/#option-scope



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