How can I revert a 'draggable' upon failed AJAX request?

Posted by Frank on Stack Overflow See other posts from Stack Overflow or by Frank
Published on 2009-08-31T13:18:11Z Indexed on 2010/06/01 10:03 UTC
Read the original article Hit count: 231

Filed under:
|
|
|

I have a draggable div, that needs to be dropped on a droppable. If it is dropped correctly, the droppable calls an AJAX request. If the draggable does not have a valid id or the AJAX request fails, the draggable should be reverted to it's original position. Is that something that can be done with jQuery?!

Any help is greatly appreciated!!

$("div.draggable").livequery(function(){
   $(this).draggable({
      revert: 'invalid',
      distance: 20
   })
});

$("#trash").droppable({
   tolerance: 'touch',
   drop: function(event, ui) 
   {    
      var item = ui.draggable;
      var id = item.attr('id');

      if (id) 
      {
         $.ajax( 
         {
            type: "POST",
            url: "delete.php",
            data: "id=" + id,
            success: function(html)
            { 
               if (html) 
               {
                  item.remove();
               } 
               else
               {
                  // FAILED AFTER AJAX
                  // is it possible to revert the draggable from here??
               } 
            }
         });    		
      }
      else
      {
         // FAILED BEFORE AJAX
         // is it possible to revert the draggable from here??
      }
}
});

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery