Reputation: 6715
I have a jquery script like this:
$(document).on('click', '.someClass', function(e){
e.preventDefault();
// Some code...
});
I would like to rewrite this as:
blah = {
init: function() {
e.preventDefault();
// Some code...
}
}
$(document).on('click', '.someClass', blah.init);
But how do I pass the e variable to the object?
Upvotes: 0
Views: 325
Reputation: 724
To use an object literal's function as an event handler's callback, you must set the parameters of the literal's function equal to the parameters which the event callback requires, if you wish to access them.
Like so:
var blah = {
init: function(e) { // we want to access the event object, so we set it as a function param
console.log(e);
alert('bye');
}
}
$(document).on('click', blah.init);
Upvotes: 1
Reputation: 4704
You need to put init
as a function :
blah = {
init: function(e) {
e.preventDefault();
// Some code...
}
}
Upvotes: 2
Reputation: 152216
Just try with:
blah = {
init: function(e) {
e.preventDefault();
// Some code...
}
}
Upvotes: 1