Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

e.target in JS vs. 'this' in jQuery

I'm wondering why 'this' isn't used instead of e.target in JS. I'm assuming that's just the way it works with eventListeners but wouldn't mind someone chiming in with some more explanation. In jQuery, $(this) is used to target the element being clicked in an event handler.

1 Answer

Steven Parker
Steven Parker
231,210 Points

You can also use $(e.target) in jQuery. As to which to use, I think it depends on programming style. You need to be consistent about declaring "e" as a parameter in your handlers to use e.target, but you don't need a declared parameter to use this. On the other hand, you can't use this if you define your handler using an arrow function.

I prefer e.target personally, but I don't know that there's any general consensus about one way or the other to be a "best practice".

That's interesting, thanks.