Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery draggable(), clone() to append div...Pls Fiddle my jsfiddle

UPDATE:

http://jsfiddle.net/wJUHF/7/
This is the updated and working fiddle for anyone that may read this.


I am trying to get this jfiddle to work.

here is where the problem lies. I can drag the image to the container. It appends a clone, with no problem. When I click to drag the cloned image in the container it works properly the first time. The second time I click to drag, it doesnt drag but clones the already cloned image. To better understand, I have created a jsfiddle. please have a look and let me know where I am going wrong here.

http://jsfiddle.net/wJUHF/

Thanks

CODE:

$(function(){  
    //Make every clone image unique.  
    var counts = [0];  
    $(".dragImg").draggable({
        helper: "clone",
        //Create counter
        start: function() { counts[0]++; }
    });

    $("#dropHere").droppable({
        drop: function(e, ui){
            $(this).append($(ui.helper).clone());
            //Pointing to the dragImg class in dropHere and add new class.
            $("#dropHere .dragImg").addClass("item-"+counts[0]);
            //Remove the current class (ui-draggable and dragImg)
            $("#dropHere .item-"+counts[0]).removeClass("dragImg ui-draggable ui-draggable-dragging");
            //not working 100%           
            $(".item-"+counts[0]).dblclick(function(){
                $(this).remove();
            });     

            //make the div draggable --- Not working???    
            make_draggable($(".item-1"));              
        }
    });

    var zIndex = 0;
    function make_draggable(elements)
    {   
        elements.draggable({
            containment:'parent',
            start:function(e,ui){ ui.helper.css('z-index',++zIndex); },
            stop:function(e,ui){}
        });
    }
});

HTML:

<body>
    <div class="dragImg"><img src="http://placehold.it/80x80">
     </div>
    <div id="dropHere"></div>
</body>

CSS:

#dropHere {
    width:400px;
    height: 400px;
    border: dotted 1px black;
}
like image 332
n00bInNeed Avatar asked Aug 28 '13 19:08

n00bInNeed


1 Answers

jQuery(".dragImg").draggable({
        //  use a helper-clone that is append to 'body' so is not 'contained' by a pane
        helper: function() {
            return jQuery(this).clone().appendTo('body').css({
                'zIndex': 5
            });
        },
        cursor: 'move',
        containment: "document"
    });

SOLVED UR PROBLEM JSFIDDLE DEMO

like image 94
Vaibs_Cool Avatar answered Oct 18 '22 04:10

Vaibs_Cool