1

I am trying to understand how to get this to work and I am unsure...

I have multiple parent divs (the droppables) and multiple child divs (the draggables).

The child divs contain a form and the form contains many input tags.

All of the input elements are required and I want user to enter a value in it. If they are empty and user does not enter value in it and if the user drags any one of them within any parent div, I want the child div to be validated. If the validation fails then I want the child div to revert back to its location.

$( ".parent" ).droppable({
        accept: ".child",
        activeClass: "ui-state-hover",
        hoverClass: "ui-state-active",
    tolerance:'pointer',
        drop: function( event, ui ) {
//validate and then reject if it fails???
            $( this )
                .addClass( "ui-state-highlight" )
                .find( "p" )
                .append(ui.draggable).animate({width:'100',height:'100'});
        }
    });

$( ".child" ).draggable();​

I can't figure out how to implement this Example:

http://jsfiddle.net/Dyawa/7/

Is it possible for child div to revert back to its original position if the name of the child is empty?

InfoLearner
  • 14,952
  • 20
  • 76
  • 124

1 Answers1

0

See this question: how to revert position of a jquery UI draggable based on condition

You need to instruct JQuery to revert the draggable div if some rule is not met.

$(".child").draggable({ revert: 'invalid' });

Then in your .droppable() sets what is valid for a drop using the accept option, for example:

$(".parent").droppable({ 
    accept: function(dropElem) {
      //dropElem was the dropped element, return true or false to accept/refuse it
      return ($(dropElem).hasClass("child") && $("input",dropElem).val().length > 0);
    },
    activeClass: "ui-state-hover",
    hoverClass: "ui-state-active",
    tolerance:'pointer',
    drop: function( event, ui ) {
        $( this )
            .addClass( "ui-state-highlight" )
            .find( "p" )
            .append(ui.draggable).animate({width:'100',height:'100'});
    }
});
Community
  • 1
  • 1
mccannf
  • 16,619
  • 3
  • 51
  • 63