this in Event Handlers and DOM in JavaScript
this in event handlers is the element, but arrows and detached handlers change that.
this in Event Handlers and DOM in JavaScript
this in event handlers depends on how you register the handler. Here is what to expect.
Regular Function Handler: this Is the Element
button.addEventListener("click", function () { console.log(this); // the button element }); `` The browser sets `this` to the element that received the event. ### Arrow Function Handler: this Is Lexical ```js button.addEventListener("click", () => { console.log(this); // the enclosing scope's this, NOT the button }); `` Arrows inherit `this` from where they were written. Use `event.currentTarget` to get the element: ```js button.addEventListener("click", (e) => { console.log(e.currentTarget); // the button }); `` ### Inline HTML Handler: this Is the Element ```js <button onclick="console.log(this)">Click</button> `` The browser wraps this in a function with `this` bound to the element. ### Detaching a Method ```js const obj = { name: "Kunal", handleClick() { console.log(this.name); }, }; button.addEventListener("click", obj.handleClick); // undefined (this is the button) `` The method is detached. `this` is the button, not `obj`. Fix with bind: ```js button.addEventListener("click", obj.handleClick.bind(obj)); // "Kunal" `` ### 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 (use `event.currentTarget`). Detached methods lose `this` (bind them). Use `event.currentTarget` for reliable element access regardless of `this`.
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.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

