Q_Mlilo
Q_Mlilo

Reputation: 1787

Detecting parent elements in drag and drop

I am writing a board game script and would like to use jQuery UI's drag and drop functionality. When i drag an element from inside DIV A to inside DIV B, is there a way of reading DIV B as the new container of the element.

$( ".draggable" ).draggable({
    stop: function() {
        alert(this.parentNode.id); // alerts DIV A's id
    }
});

Upvotes: 1

Views: 270

Answers (2)

SteD
SteD

Reputation: 14027

If you don't declare div b as droppable, you're actually not dropping div a inside div b. You are only changing the location of div a by moving it around.

Try this, declare div b as droppable.

 $("#b").droppable();
 $("#a").draggable();

$( "#b" ).droppable({
    drop: function( event, ui ) {
        alert($(this).attr("id"));
    }
});

Upvotes: 1

Neil
Neil

Reputation: 5782

alert($(this).closest('div').attr('id'))

Try this.

Upvotes: 0

Related Questions