user1184100
user1184100

Reputation: 6894

Passing a variable to event in drag and drop

I have multiple droppable areas ..

for(j=0; j<2; j++) {
$('#dropElement' + j).droppable( {          
        drop : handleElementDrop
    });

}

function handleElementDrop( event, ui ) { 
}

How can I pass 'j' to event handleElementDrop so that i can know to which area the element was dropped to .. ?

Upvotes: 0

Views: 2871

Answers (3)

techfoobar
techfoobar

Reputation: 66663

You can use jQuery's data() function to transport any data (not just integeres, even arrays or objects) within elements..

For ex:

for(j=0; j<2; j++) {

    $('#dropElement' + j).data('mykey', myData); // set data

    $('#dropElement' + j).droppable( {        
        drop : handleElementDrop
    });
}

function handleElementDrop( event, ui ) { 

    var myData = $(this).data('mykey'); // get data

}

Upvotes: 1

Simon Edstr&#246;m
Simon Edstr&#246;m

Reputation: 6619

You can use this inside the handleElementDrop to get the dropped. I should store the ID inside a data attribute. But however this will work for you:

for(j=0; j<2; j++) {
    $('#dropElement' + j).droppable( {          
        drop : handleElementDrop
    });

}

function handleElementDrop( event, ui ) { 
    alert(this."id".replace("dropElement",""))
}

http://jsfiddle.net/Aspaq/

Upvotes: 0

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76870

I think you could do

    drop: function(event, ui) {
        alert(this.id);
    }

to find the id of the droppable (so you now which droppable has been used)

fiddle here http://jsfiddle.net/r5rzX/

Upvotes: 1

Related Questions