Reputation: 251
I bulid a function to event and I want to know how can I pass parameters to that function?
I use addEventListener
method to add events
What I need is to pass the element
variable to the function mean this
object
For example if I use attribute method to add event I do like this:
<div onclick="function(this)">
And then the function
will get the div element
Now my question is how can I pass the this object to the function when I use addEventListener
Is there anyway to get this thing?
Upvotes: 0
Views: 53
Reputation: 2788
Blah.addEventListener(function(event){
var element = event.target;
});
Posted from phone. Please forgive
Upvotes: 1
Reputation: 10929
In an event handler, such as onclick, 'this' will automatically point to the element, where the event is triggered. You can use it inside your anonymous function. Note: It's not a string but a HtmlElement. Hope I have understood your question correctly.
Upvotes: 0
Reputation: 469
http://www.w3schools.com/jsref/met_document_addeventlistener.asp
Define your listener with a parameter, this will be an event object. In this object you will find the triggered element.
Upvotes: 0