+ 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.

2nd Nov 2020, 8:22 AM
Fasasi Sherif
Fasasi Sherif - avatar
4 odpowiedzi
+ 2
Okay, thanks. Matiyas Sirak
2nd Nov 2020, 10:17 AM
Fasasi Sherif
Fasasi Sherif - avatar
+ 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.
2nd Nov 2020, 8:31 AM
Fasasi Sherif
Fasasi Sherif - avatar
+ 1
Plz I haven't still gotten it Nasif Rahman
2nd Nov 2020, 8:41 AM
Fasasi Sherif
Fasasi Sherif - avatar
+ 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); }
2nd Nov 2020, 8:53 AM
Matias
Matias - avatar