Coder
Coder

Reputation: 219

How to drag and drop multiple divs in one div using javascript

I'm having trouble on a project I mentioned earlier, now the problem is that I want to drag a div and drop it to another div. If I drag a div then it should be added below the previously dropped div. How can I achieve that? My current code is not appending the next div dropped, it overwrites the first one. I have tried many solutions but all end up doing more damage than good. Can anybody please help me, Thank you.

<html>
<head>
    <title>CRM</title>
    <link rel="stylesheet" href="style/style.css" type="text/css">
    <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
    <script src="//code.jquery.com/jquery-1.10.2.js"></script>

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.js"></script>


    <script>
    $(document).ready(function(){

        $(".dragable").draggable({
            cancel:"a.ui-icon",
            revert:true,
            helper:"clone",
            cursor:"move",
            revertDuration:0
        });

    $('.droppable').droppable({
        accept: ".dragable",
        activeClass: "ui-state-highlight",
        drop: function( event, ui ) {
        // clone item to retain in original "list"
        var $item = ui.draggable.clone();

        $(this).addClass('has-drop').html($item);

    }
});
    });
    </script>

</head>

<body>
    <div class="main-container">
        <div class="wrapper">
            <div class="tb-head">

                <div class="target">
                    <span class="target-span">Target</span>
                </div>

                <div class="user1">
                    <span class="user1-span">User1</span>
                </div>

                <div class="user2">
                    <span class="user2-span">User2</span>
                </div>

                <div class="user3">
                    <span class="user3-span">User3</span>
                </div>

                <div class="user4">
                    <span class="user4-span">User4</span>
                </div>

                <div class="clear"></div>
            </div>

            <div class="tb-body">

                <div class="inner-target">

                    <div class="dragable">
                        <span class="targetinn-span">Target Lead</span>
                    </div>

                    <div class="dragable">
                        <span class="targetinn-span">Assign1 Lead</span>
                    </div>

                    <div class="dragable">
                        <span class="targetinn-span">Assign2 Lead</span>
                    </div>

                    <div class="dragable">
                        <span class="targetinn-span">Assign3 Lead</span>
                    </div>

                </div>

                <div class="inner-user1">
                    <div class="droppable">
                        <span class="user1inn-span"></span>
                    </div>
                </div>

                <div class="inner-user2">
                    <div class="droppable">
                        <span class="user2inn-span"></span>
                    </div>
                </div>

                <div class="inner-user3">
                    <div class="droppable">
                        <span class="user3inn-span"></span>
                    </div>
                </div>

                <div class="inner-user4">
                    <div class="droppable">
                        <span class="user4inn-span"></span>
                    </div>
                </div>

                <div class="clear"></div>
            </div>
    </div>

</body>
</html>

Upvotes: 2

Views: 3365

Answers (1)

xxxmatko
xxxmatko

Reputation: 4142

Just change this line of code

$(this).addClass('has-drop').html($item);

to this

$(this).addClass('has-drop').append($item);

By calling .html() you replaced the original html just with last dropped element.

Upvotes: 1

Related Questions