Harman met
Harman met

Reputation: 251

Pass paramtes to event function

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

Answers (3)

QBM5
QBM5

Reputation: 2788

Blah.addEventListener(function(event){
     var element = event.target;

});

Posted from phone. Please forgive

Upvotes: 1

Poul Bak
Poul Bak

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

fucethebads
fucethebads

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

Related Questions