The situation is somewhat like-
var someVar = some_other_function(); someObj.addEventListener("click", function(){ some_function(someVar); }, false);
The problem is that the value of someVar
is not visible inside the listener function of the addEventListener
, where it is probably being treated as a new variable.
With the addEventListener() method you can specify the propagation type by using the "useCapture" parameter: addEventListener(event, function, useCapture); The default value is false, which will use the bubbling propagation, when the value is set to true, the event uses the capturing propagation.
addEventListener listens for both capture phase and bubbling phase events. The third parameter (called useCapture in the specification) allows the programmer to specify which phase they want to use. In modern browsers, this defaults to false .
Why not just get the arguments from the target attribute of the event?
Example:
const someInput = document.querySelector('button'); someInput.addEventListener('click', myFunc, false); someInput.myParam = 'This is my parameter'; function myFunc(evt) { window.alert(evt.currentTarget.myParam); }
<button class="input">Show parameter</button>
JavaScript is a prototype-oriented language, remember!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With