I want to make an eventHandler that passes the event and some parameters. The problem is that the function doesn't get the element. Here is an example:
doClick = function(func){
var elem = .. // the element where it is all about
elem.onclick = function(e){
func(e, elem);
}
}
doClick(function(e, element){
// do stuff with element and the event
});
The 'elem' must be defined outside of anonymous function. How can i get the passed element to use within the anonymous function? Is there a way to do this?
And what about addEventListener? I don't seem to be able to pass the event through an addEventListener at all do I ?
Update
I seemed to fix the problem with 'this'
doClick = function(func){
var that = this;
this.element.onclick = function(e){
func(e, that);
}
}
Where this contains this.element that i can access in the function.
The addEventListener
But i'm wondering about the addEventListener:
function doClick(elem, func){
element.addEventListener('click', func(event, elem), false);
}