+ 1
How do I pass parameters to addEventListeners?
Please how do I pass parameters to an addEventListener and later pass a value the parameters when I click on it? I've googled it and checked stack overflow, but I still don't know how to go about this.
4 Réponses
+ 2
Okay, thanks. Matiyas Sirak
+ 1
No, I meant I want to pass parameters to the function itself. For example:
btn.addEventListener("click", myFunc(someVar))
And then change the value of someVar itself when I click on it. I know this doesn't work.
+ 1
Plz I haven't still gotten it Nasif Rahman
+ 1
Try this code
const someInput = document.querySelector('button');
someInput.addEventListener('click', myFunc, false);
someInput.myParam = 'This is my parameter';
The argument on addeventlisterner is called here in the function👇
function myFunc(evt) {
window.alert(evt.currentTarget.myParam);
}