NateW
NateW

Reputation: 2121

jQuery draggable constrain to parent's parent

I would like to constrain the movement of my draggable div within the parent of the parent.

<div class="grandparent'> // <-this is the div I would like to constrain to
  <div class="parent">
    <div class="draggable">
    ...
    </div>
  </div>
</div>

I know that I can contain within the parent like this:

$( ".draggable" ).draggable({
  containment: "parent"
});

And I know that I can contain within a specific div by passing in an id, but in my case I can't do that(id's of the grandparent are being dynamically generated).

How can I contain within the parent's parent?

Upvotes: 3

Views: 1177

Answers (1)

Павел Иванов
Павел Иванов

Reputation: 1154

$('.draggable').each(function(){
  var $el = $(this);
   $el.draggable({containment:$el.closest('.grandparent')    });
});

Use parents object find and for each iterator

Upvotes: 4

Related Questions