Using the code snippet below I am trying to bind two events on my element, click and hover, and I am passing the eventName variable to the module.execute() function. But in both cases this variable that I pass eventName equals to be "click". I understand closures and I can see why eventName is always "click". What would be an elegant solution to ensure that eventName refers to "hover" when the hover callback is called and "click" when the click callback is called.
function foo() {
var events = ["hover", "click"];
for (var j = 0; j < events.length ; j++){
var eventName = events[j];
if(eventName === "hover"){
$("#element").hover(function(){
//when this runs eventName is always click
module.execute(eventName);
});
}
else{
$("#element").click(function(){
module.execute(eventName);
});
}
}
}