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 jQuery Basics (2014) Creating a Spoiler Revealer Perfect

Terrence Autry
Terrence Autry
3,170 Points

How do you know what element "this" is referring too?

I think the first "this" refers to the span element, and the second "this" refers to the button. I'm trying to understand how you know which "this" refers to which element.

1 Answer

andren
andren
28,558 Points

In the context of direct jQuery event handlers this always refers to the element that the handler is attached to. So this refers to the button on both of the lines. The reason why the first line ends up dealing with the span element is that the prev method is called on it.

prev is a jQuery method that returns the sibling element that precedes the element you call it on, since the button comes after the span element that is the element that the prev method returns. Then show is called on that element.