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

Jimmy Names
Jimmy Names
10,371 Points

why use (this) and not (.button)?

In last task why do we use /this/ and not select the class for the button??

Thanks

3 Answers

LaVaughn Haynes
LaVaughn Haynes
12,397 Points

$(this) selects the specific button that was clicked. $('button') selects ALL of the buttons. If you wanted to (for example) hide each button after it was clicked and you didn't use this, then you'd hide all of the buttons after the first one was clicked.

Brendan Whiting
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Brendan Whiting
Front End Web Development Techdegree Graduate 84,738 Points

LaVaughn is absolutely right, but I would clarify that the question was about the .button class, not the button element. However, you're going to have the same problem. It's possible that multiple buttons could have that class. Rather than making programming that runs on something unique, like an ID, $(this) allows you to reuse the same code that works on every button.