Welcome to the Treehouse Community
The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.
'this' == event.target ?
In the previous video, andren said
"this within a jQuery event handler essentially maps to event.currentTarget.
The difference between currentTarget and target is that currentTarget will always refer to the element that the event listener is attached to, the p elements with the spoiler class in this case. Whereas target refers to the specific element that actually triggered the event, which in this case is the button element inside the p element.
So in this video this/event.currentTarget points to the p element that contains the button while event.target points to the button itself."
and the Jquery documentations says so too, as far as I could investigate. But in this video Treasure says this == event.target, uses it like that in the code, and seems to work. I'm a bit confused, shouldn't this point to the p element?
Steven Parker220,865 Points
Are you sure andren appears in a Treehouse video? Can you provide a link to it?
But according to the jQuery documentation:
When jQuery calls a handler, the this keyword is a reference to the element where the event is being delivered; for directly bound events this is the element where the event was attached and for delegated events this is an element matching selector. (Note that this may not be equal to event.target if the event has bubbled from a descendant element.)
So it seems the type (direct or delegated) of handler will determine if "this" will be equal to the
target or the