3

Here is my code:

$(document).ready(function(){
dropbox.addEventListener("dragenter", somethingHappens, false);
});

function somethingHappens(evt) {
    // code here
}

How can I pass data to the somethingHappens() function when attaching the event listener? Is that possible? I was looking for something I could do like:

function somethingHappens(evt,dataItem1,dataItem2) {
    // code here, I need to use evt here
}

2 Answers 2

4

You can do it the old school way if you don’t mind using an anonymous function as handler:

var data = { foo: 'bar' };

dropbox.addEventListener("dragenter", function(evt) {
    somethingHappens.call(this, evt, data);
}, false);

function somethingHappens(evt, data) {
    console.log(data);
}

But I’m also pretty sure you can use jQuery events for this:

$(dropbox).bind( 'dragenter', { foo: 'bar' }, somethingHappens );

function somethingHappens(evt) {
    console.log(evt.data);
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could use jQuery's bind method:-

$(document).ready(function(){
$('#yourdropboxid').bind("dragenter", { foo: 'bar' }, somethingHappens);
});

function somethingHappens(event) {
   alert(event.data.foo); // alerts 'bar'
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.