How to pass arguments to addEventListener listener function?
There is absolutely nothing wrong with the code you've written. Both some_function
and someVar
should be accessible, in case they were available in the context where anonymous
function() { some_function(someVar); }
was created.
Check if the alert gives you the value you've been looking for, be sure it will be accessible in the scope of anonymous function (unless you have more code that operates on the same someVar
variable next to the call to addEventListener
)
var someVar;
someVar = some_other_function();
alert(someVar);
someObj.addEventListener("click", function(){
some_function(someVar);
}, false);
How to pass arguments to addEventListener listener function with typescript?
Just wrap the actual event handler in an anonymous function and that function can then call your function with its arguments. But don't forget that event handlers are automatically passed a reference to the event that triggered them, so capture that in the outer handler and pass it along with your other arguments:
document.addEventListener("keyup", function(event){ searchKeyPressHandler(event, x,y,z); });
Or, if you need to utilize the function that you are setting up as the "wrapper" more than once (in the case of having to remove the listener later), you'd just declare the function with a name and pass that name into .addEventListner()
as follows:
function handler(event){
searchKeyPressHandler(event, x,y,z);
}
document.addEventListener("keyup", handler);
// And later...
document.removeEventListener("keyup", handler);
How to pass arguments to addEventListener for google maps Polygon listener function in addition to the event?
The Polygon
's click
event listener takes only one parameter. That means the library passes no more than one parameter when it calls the function.
If the information you need is not attached to the event
object, you have to retrieve it by other means.
If you need to pass the parameters yourself, you can write a higher order function:
function showArrays(area) {
return function (event) {
let contentString =
'<div class="form-group pr-2 pl-2">' +
'<div class="text-dark">name:</div>' +
area.name +
"<br>" +
'<div class="text-dark">shipping_cost :</div>' +
area.shipping_cost +
"$" +
"<br></div>";
// Replace the info window's content and position.
infoWindow.setContent(contentString);
infoWindow.setPosition(event.latLng);
infoWindow.open(map);
};
}
bermudaTriangle.addListener("click", showArrays(area));
JavaScript addEventListener function with parameters
you can use () => { call your function here } or function(){ call your function here}
document.getElementById("button1")
.addEventListener("click", () => { test1("four");});
function test1(text) {
console.log(text);
}
<button id="button1">Click Me</button>
How to pass parameter to function using in addEventListener?
No need to pass anything in. The function used for addEventListener
will automatically have this
bound to the current element. Simply use this
in your function:
productLineSelect.addEventListener('change', getSelection, false);
function getSelection() {
var value = this.options[this.selectedIndex].value;
alert(value);
}
Here's the fiddle: http://jsfiddle.net/dJ4Wm/
If you want to pass arbitrary data to the function, wrap it in your own anonymous function call:
productLineSelect.addEventListener('change', function() {
foo('bar');
}, false);
function foo(message) {
alert(message);
}
Here's the fiddle: http://jsfiddle.net/t4Gun/
If you want to set the value of this
manually, you can use the call
method to call the function:
var self = this;
productLineSelect.addEventListener('change', function() {
getSelection.call(self);
// This'll set the `this` value inside of `getSelection` to `self`
}, false);
function getSelection() {
var value = this.options[this.selectedIndex].value;
alert(value);
}
How to pass arguments to addEventListener listener function?
There is absolutely nothing wrong with the code you've written. Both some_function
and someVar
should be accessible, in case they were available in the context where anonymous
function() { some_function(someVar); }
was created.
Check if the alert gives you the value you've been looking for, be sure it will be accessible in the scope of anonymous function (unless you have more code that operates on the same someVar
variable next to the call to addEventListener
)
var someVar;
someVar = some_other_function();
alert(someVar);
someObj.addEventListener("click", function(){
some_function(someVar);
}, false);
Changing Iframe size by click event listener not working
change your event listener function like so
btn.addEventListener('click', () => change(150));
OR
btn.addEventListener('click', function() { change(150) });
addEventListener
expects a function definition and not a function call as second argument.
For more read: https://www.w3schools.com/js/js_htmldom_eventlistener.asp
Related Topics
Create a Newline for Every X Number of Characters
React How to Fix Failed Prop Type - Invalid Prop of Type String Expected Object
How Does Group by Works in Sequelize
Calculating Sum of Checked Checkboxes
Change Image on Scroll Position
Show 2 Items Per Row[React Native]
Display Data Label (Legend) in Line-Chart Using Chartjs
Javascript Loop Wait Until API Call Finished
How to Detect the Mime Type of Data Url
Extracting Key:Value Pairs Assoc With Regex from String on JavaScript
Replace Unicode Matches in JavaScript
Why the Value of Input Elements Are Not Changing
How to Update Component State on Redux State Change
React Component Not Refreshing When Redux State Has Been Updated With Successful Action
Disable F5 and Browser Refresh Using JavaScript