Facebook Pixel

How this Works in JavaScript Event Handlers

Event handlers set this to the element, but arrows and detached handlers change that. Here is what to expect.

How this Works in JavaScript Event Handlers

Event handlers are a common place where this causes confusion. The rules depend on how you register the handler.

Regular Function Handler: this Is the Element

const btn = document.querySelector("button"); btn.addEventListener("click", function () { console.log(this); // the button element });

When the browser calls the handler, it sets this to the element that received the event.

Arrow Function Handler: this Is Lexical

btn.addEventListener("click", () => { console.log(this); // the enclosing scope's this, NOT the button });

Arrows do not get this from the call. They inherit from where they were written. If you need the element, use event.currentTarget:

btn.addEventListener("click", (e) => { console.log(e.currentTarget); // the button });

Inline HTML Handler: this Is the Element

<button onclick="console.log(this)">Click</button>

The browser wraps this in a function with this bound to the element.

jQuery-Style Handlers

In jQuery, this in a handler is the element matched by the selector, not always event.currentTarget. Be aware of the library you use.

Detaching a Method

const obj = { name: "Kunal", handleClick() { console.log(this.name); }, }; btn.addEventListener("click", obj.handleClick); // undefined

The method is detached from obj when passed. The browser calls it with this as the button, not obj. Fix with bind or an arrow:

btn.addEventListener("click", obj.handleClick.bind(obj)); btn.addEventListener("click", () => obj.handleClick());

Best Practice

  • Use event.currentTarget to get the element, regardless of this.
  • Use arrows if you want the enclosing this (e.g., a class method).
  • Use regular functions if you want this to be the element.
  • Always bind detached methods.

The Takeaway

In regular function event handlers, this is the element. In arrow handlers, this is lexical (not the element; use event.currentTarget). Detached methods lose their this; bind them or wrap in an arrow. Prefer event.currentTarget for the element.

The element that received the event. When the browser calls a regular function handler, it sets this to the element registered with addEventListener.

The enclosing lexical scope's this, not the element. Arrow functions do not get this from the call. Use event.currentTarget to access the element inside an arrow handler.

Because the method is detached from obj when passed as a reference. The browser calls it with this as the element, not obj. Fix with .bind(obj) or an arrow wrapper () => obj.method().

Use event.currentTarget. It always refers to the element the listener was registered on, regardless of whether this is the element (regular function) or lexical (arrow function).

The element. The browser wraps an inline handler like onclick="..." in a function with this bound to the element that has the attribute.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.