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 2

I didn't got, why we use $(".spoiler span") instead of $(" span")?

I didn't got, why we use $(".spoiler span") instead of $(" span")? or why dont we use $(".spoiler") instead of $(".spoiler span") ?

1 Answer

Matt F.
Matt F.
9,518 Points

Hi Javid,

Spoiler alongside span is simply more selective - if you had multiple spans on the page, this would make it so the only spand that is being affected is the one with the class of spoiler.

Similarly, if you later decided to wrap the span along with information that should not be affected by the jQuery, inside a div with a class of spoiler, it would be unintentionally affected by the jQuery.

Even if none of those cases exist in the current code, it is always better to be more selective so you do not have to fix a bug that occurs when expanding the code base.