Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In jQuery, how to revert a draggable on ajax call failure?

I want the draggable to be reverted to its original position if the ajax call on drop returns a failure. Here is the code what I am imagining it to be.. It is OK if the draggable rests in the droppable while the ajax call is in process...

<script type="text/javascript">
jQuery(document).ready($){
    $("#dragMe").draggable();
    $("#dropHere").droppable({
        drop: function(){
            // make ajax call here. check if it returns success.
            // make draggable to return to its old position on failure.
        }
    });
}
</script>
<div id="dragMe">DragMe</div>
<div id="dropHere">DropHere</div>
like image 895
NikhilWanpal Avatar asked Mar 15 '11 12:03

NikhilWanpal


1 Answers

Try to save the original position before starting to drag and restore it if drops fail. You can save the original position like this:

var dragposition = '';

$('#divdrag').draggable({
   // options...
   start: function(event,ui){
      dragposition = ui.position;
   }
});

$("#dropHere").droppable({
   drop: function(){
       $.ajax({
           url: 'myurl.php',
           data: 'html',
           async: true,
           error: function(){
               $('#divdrag').css({
                  'left': dragposition.left,
                  'top': dragposition.top
               });
           }
       });
   }
});
like image 200
Fran Verona Avatar answered Sep 21 '22 03:09

Fran Verona