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 Perform: Part 3

Yashwanth Yash
Yashwanth Yash
10,947 Points

Why shouldn't we use button instead this?

I have used "button" selector instead of "this" and it works the same. Is there any disadvantage of using button?

2 Answers

Konstantin Kovar
Konstantin Kovar
16,003 Points

If you were to add another button to your page, it would disappear then aswell. In fact ALL the elements with that specific tag would would be the target of whatever function you're calling on it, which can be what you want in some cases.

The "this" keyword makes sure only this one specific element is targeted.

Jenna Malone
Jenna Malone
7,173 Points

Yeah but cant you just get more specific? I used: $(.spoiler button).remove(); and it did the same thing. I also used the original method he taught us hide instead of remove and it still worked.

Konstantin Kovar
Konstantin Kovar
16,003 Points

You could do something like that, but then you would have to rewrite this for every specific button. Remember to keep your code DRY (don't repeat yourself).

Jenna Malone
Jenna Malone
7,173 Points

Oh ok. Makes sense. Thank you!