Lets say I want to pass arguments to my function. I hear that I should do it by calling another function from within a function like this:
myElement.addEventListener("click",function whatever(event){
myFunk(event,"some argument");
},false);
But what now if I want to remove it?
myElement.removeEventListener("click",whatever,false);
var myElement = document.querySelector('div')
function myFunk(e,m){
console.log(e,m)
}
myElement.addEventListener("click",function whatever(event){
myFunk(event,"some argument")
},false)
myElement.removeEventListener("click", whatever, false)
<div></div>
This returns a "whatever is not defined" error in the console. So does my function not have a name? Is "whatever" not its name? How now do I remove it?
I need a way to assign an event listener and pass it arguments, and then get rid of it later. How do I do this?
EDIT:
Okay, yes, I cannot refer to whatever because it is written inline. But the reason I am forced to use a function expression inline is because I want to pass it arguments. I want to pass it arguments because I don't want to have to define a new function for every time I want to change the behaviour. If I define an event listener using text on the "on" property I can easily swap out parameters:
element.onClick("myFunc('do one thing')");
element.onClick("myFunc('do a different thing')");
How do I do this with addEventListener, inline, removable, and with dynamic parameters?
whatevernot function, but function expression, so after callingmyElement.addEventListenerfunctionwhateveris not available, but you can usewhateverinside likefunction whatever(){...; whatever();}