1

As described, i want to be able to drag some divs from a sort of menu that i have, and be able to drop them to a specific div.. i managed to do sort of it following this..

http://jsfiddle.net/bysnc/

however, i want to add a "x" button on each one, so that onclick of the "x" button the draggable/droppable element to return (revert) to its original position..

i had found this

http://jsfiddle.net/v7n6w/

which seemed kind of what i wanted (actually i want a unique button for each and not a overall one.. but 1) this doesn't seem to be dropped even on jsfiddle 2) on my test, i get an error in the console "Uncaught TypeError: Cannot read property 'originalPosition' of undefined "

and specifically the error gets in this line ui.draggable.data("draggable").originalPosition);

here is the js part..

function revertDraggable($selector) {
    $selector.each(function() {
        var $this = $(this),
            position = $this.data("originalPosition");

        if (position) {
            $this.animate({
                left: position.left,
                top: position.top
            }, 500, function() {
                $this.data("originalPosition", null);
            });
        }
    });
}   

$(document).ready(function() {
    $("#drag").draggable({
        revert: "invalid"
    });

    $("#floor").droppable({
        drop: function(event, ui) {
            if (!ui.draggable.data("originalPosition")) {
                ui.draggable.data("originalPosition",
                    ui.draggable.data("draggable").originalPosition);
            }
        }
    });

    $("#other").click(function() {
        revertDraggable($("#drag"));
    });

});

and my html looks like this

droppable position i want to be able to be dropped only in the foo

<div id="container">
    <div id="floor">foo</div>
    <div id="other">bar</div>
</div>

and the initial position

 <div class="menu" id="power" width="300">
<div class="options ui-widget-content" id="drag"><img src="1.jpg"/></div>
    <div class="options ui-widget-content" id="drag2"><img src="2.jpg"/></div>
</div>

p.s. can this be down with just jquery and not plugin jquery.ui?

nikolas
  • 723
  • 2
  • 17
  • 37

1 Answers1

1

Mixed code from 2 samples

    $('#floor').droppable({
    tolerance: 'fit'
});

$('.draggable-div').draggable({
    revert: 'invalid',
    stop: function(){
        $(this).draggable('option','revert','invalid');
        $(this).find('.undo').show();
    }
});

$('.draggable-div').find('.undo').click(function(i, e) {
    var $div = $(this).parent();
    revertDraggable($div);
});

$('#floor').droppable({
    greedy: true,
    tolerance: 'touch',
    drop: function(event,ui){
       // ui.draggable.draggable('option','revert',true);
        if (!ui.draggable.data("originalPosition")) {
        ui.draggable.data("originalPosition",
                          ui.draggable.data("draggable").originalPosition);
        
        }


        $(this).find('.undo').show();
    }
});

function revertDraggable($selector) {
    $selector.each(function() {
        var $this = $(this),
            position = $this.data("originalPosition");

        if (position) {
            $this.animate({
                left: position.left,
                top: position.top
            }, 500, function() {
                $this.data("originalPosition", null);
            });
        }
    });
    
    $selector.find('.undo').hide();
}

See full code here.

It works now

aki
  • 164
  • 1
  • 1
  • 12
Draykos
  • 773
  • 7
  • 16
  • thank you for the response mate...but for some reason, i keep getting an error Uncaught TypeError: Cannot read property 'originalPosition' of undefined when i move the draggable into the "floor" div – nikolas Apr 04 '13 at 09:47
  • Which browser? and: Are you trying my sample? – Draykos Apr 04 '13 at 09:51
  • in jsfiddle works great, when i paste the code in the overall site, i just get this error..it does gets and drops, but ends there..nothing happens next..both chrome and firefox – nikolas Apr 04 '13 at 09:57
  • i pasted it also in blank document, and i still get the same issue :S http://jqueryui.com/download/ 1.10.2 – nikolas Apr 04 '13 at 10:03
  • Not easy to help you. I'll try a suggest: check at jquery.ui script. If it is a custom built, maybe it doesn't have all the required widgets. Try to replace with a clean version. For example, download it from jsfiddle site. Then try the same with a clean version of jquery: if it's working on fiddle it must work everywhere. – Draykos Apr 04 '13 at 10:03
  • 1
    in fiddle sample you used: jquery 1.5.2 and jquery-ui 1.8.9. Use same versions or update both jquery and jquery-ui. Here a version with plugin updated: http://jsfiddle.net/d9BVF/3/ – Draykos Apr 04 '13 at 10:08
  • i had jquery 1.9.1 and j ui 1.10.2 when i changed just the jquery.ui i got a msie error, which was from jquery.. so i downgraded jquery to 1.5.2 and now it works... :S i really appreciate your help mate.. i can't get though why newr versions don't support – nikolas Apr 04 '13 at 10:11