user1001176
user1001176

Reputation: 1163

jquery drag n drop revert back to original position not working

I am making a list of products which can be dragged from left container to the drop area . The products are being dragged onto the drop container but after that a (X) close button gets active on which we click then the product is reverted back to its original position . The part till dragging the product and dropping it into the container works fine . but after that i get this error

Uncaught TypeError: Cannot read property 'originalPosition' of undefined 

my code ( jquery version 1.9 and jquery ui version 1.10.3 )

     $(document).ready(function () {
$('.drg-bx1').droppable({
    tolerance: 'fit'
});

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

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

$('.drg-bx1').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();

}

Upvotes: 2

Views: 8680

Answers (1)

coding_idiot
coding_idiot

Reputation: 13734

Use ui.draggable.data("uiDraggable").originalPosition instead of

ui.draggable.data("draggable").originalPosition);

Full Answer Here

Upvotes: 3

Related Questions