Jump to content

[Javascript] Pass variable when adding Event Listener

This is a bit vague to me, I'm guessing you want to pass a variable to an existing function to use as an event listener. 

You could do this with an anonymous function, but this means you won't be able to remove the event listener later on.

let variable = "test";
element.addEventListener("click", () => listenerFunction(variable));

🙂

Link to post
Share on other sites

2 minutes ago, duncannah said:

This is a bit vague to me, I'm guessing you want to pass a variable to an existing function to use as an event listener. 

You could do this with an anonymous function, but this means you won't be able to remove the event listener later on.

let variable = "test";
element.addEventListener("click", () => listenerFunction(variable));

btw I wanan pass the object itself so does it make this easier?

Link to post
Share on other sites

1 minute ago, Wictorian said:

btw I wanan pass the object itself so does it make this easier?

You mean the event object? If that's the only thing you need, you don't have to use an anonymous function for that; the function will be called with the event object as the first argument.

 

function listenerFunc(event) {
	let element = event.currentTarget;
  
  	// and so on
}

document.getElementById("element").addEventListener("click", listenerFunc);

 

🙂

Link to post
Share on other sites

4 minutes ago, duncannah said:

You mean the event object? If that's the only thing you need, you don't have to use an anonymous function for that; the function will be called with the event object as the first argument.

 

function listenerFunc(event) {
	let element = event.currentTarget;
  
  	// and so on
}

document.getElementById("element").addEventListener("click", listenerFunc);

 

no I have a list containing instances of a class and they have a button and I wanan add event listeners to those buttons using a for loop.

Link to post
Share on other sites

3 minutes ago, Wictorian said:

no I have a list containing instances of a class and they have a button and I wanan add event listeners to those buttons using a for loop.

Oh, of course, you should be able to do that using my first example. But also, if you're attaching a method of the class as the event listener, the method should be able to access the class using the `this` keyword, which is a better way of doing things

🙂

Link to post
Share on other sites

2 minutes ago, duncannah said:

Oh, of course, you should be able to do that using my first example. But also, if you're attaching a method of the class as the event listener, the method should be able to access the class using the `this` keyword, which is a better way of doing things

I'm not doing that but I can probably arrange some things. Thanks.

Link to post
Share on other sites

21 minutes ago, duncannah said:

Oh, of course, you should be able to do that using my first example. But also, if you're attaching a method of the class as the event listener, the method should be able to access the class using the `this` keyword, which is a better way of doing things

this passes the button itself however I wanna pass the class the button is part of.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×