Friday, September 5, 2025
HomeLanguagesJavascriptscript.aculo.us Drag & Drop Containment Option

script.aculo.us Drag & Drop Containment Option

This script.aculo.us Drag & Drop Containment Option is used to create an array of elements. That has to be parented and the drop area will only accept those, you can drag a draggable element to a drop area. In that drop area, the element will be placed if the Containment is the same as the id of your draggable element’s parent.

Syntax:

Droppables.add('element', 
    {containment: element ID or array of parent's IDs}
);

Values:

  • ID: This option takes element ID or array of parent’s IDs.

Example: In this example, the second image is accepted by the drop area, the reason for that is the parent of the second image’s Containment if ‘gfg’. Each child of that parent will be accepted by the drop area.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <script type="text/javascript"
        src="prototype.js">
    </script>
     
    <script type="text/javascript"
        src="scriptaculous.js">
    </script>
     
    <script type="text/javascript">
        window.onload = function () {
            $A($('draggables').getElementsByTagName('img'))
                .each(function (item) {
                    new Draggable(item, {
                        revert: true,
                        ghosting: true
                    });
                });
 
            $A($('gfg').getElementsByTagName('img'))
                .each(function (item) {
                    new Draggable(item, {
                        revert: true,
                        ghosting: true
                    });
                });
 
            Droppables.add('droparea', {
                 hoverclass: 'hoverActive',
                 containment: 'gfg',
                 onDrop: moveItem
            });
 
            // Set drop area default non cleared.
            $('droparea').cleared = false;
        }
 
        function moveItem(draggable, droparea) {
            if (!droparea.cleared) {
                droparea.innerHTML = '';
                droparea.cleared = true;
            }
 
            draggable.parentNode.removeChild(draggable);
            droparea.appendChild(draggable);
        }
    </script>
 
    <style type="text/css">
        #draggables {
            width: 550px;
            height: 73px;
        }
 
        #gfg {
            width: 550px;
            height: 73px;
        }
 
        #droparea {
            float: left;
            width: 650px;
            height: 90px;
            border: 2px solid gray;
            text-align: center;
            font-size: 16px;
            padding: 12px;
        }
    </style>
</head>
 
<body>
    <div>
        <h1 style="color: green">
            neveropen
        </h1>
 
         
<p>A Computer Science Portal for Geeks</p>
 
    </div>
 
    <strong>
        script.aculo.us Drag & Drop
        Containment Option
    </strong>
     
    <div id="draggables">
        <img src=
    </div>
    <br><br>
 
    <div id="gfg">
        <img src=
    </div>
 
    <br><br><br><br><br>
    <div id="droparea">
        Drag the Image and Drop Your
        Image in this area
    </div>
</body>
 
</html>


Output:

  • Before drag and drop:

  • After drag and drop:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6634 POSTS0 COMMENTS
Nicole Veronica
11801 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11863 POSTS0 COMMENTS
Shaida Kate Naidoo
6750 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6701 POSTS0 COMMENTS
Umr Jansen
6718 POSTS0 COMMENTS