Deepak singh
Deepak singh

Reputation: 35

Is there any way to access the element to which the event listener is added in the function?

I want to use element in the event listener function without declaring it elsewhere in Javascript.

element.addEventListener("click",function(){
//want to access element here
})

Upvotes: 1

Views: 510

Answers (1)

Majed Badawi
Majed Badawi

Reputation: 28424

You can do this in two ways:

element.addEventListener("click",function(e){
     console.log(this);
     console.log(e.currentTarget);
})
<button id="element">Click</button>

Upvotes: 2

Related Questions