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 Simple Lightbox Adding New Attribute Values with attr()

Shouldn't $('.external a') and $('a.external') evaluate the same? Using the first class selector fails the challenge.

Am I mistaken that elements can be selected in either of the following formats?

$(.example p) or $(p.example)

The instructor actually uses the first of these two conventions. However, the exact same answer in that format fails the code challenge.

Thanks! Dan

2 Answers

Erik McClintock
Erik McClintock
45,783 Points


It's easy to get confused with these selectors, as they do look similar. The key to understanding their differences is remembering that order matters! For consistency and simplicity, JavaScript (and jQuery) use the same selector formatting as CSS.

".example p" says "give me all of the 'p' elements contained within the '.example' element." Due to the order they are in, we are looking first at the ".example" element, then for all "p" elements contained inside it.

"p.example", on the other hand, says "look at all 'p' elements that have the class '.example'."

This is the same way that you would target things in your CSS. If you want to target all paragraph elements, you would write:

(in CSS)

/* targets all p elements */
p {

(in jQuery)

$( 'p' );

If, instead, you only wanted to target the paragraph elements that contained the class "special", you would write:

(in CSS)

/* targets only the p elements that have the class "special" */
p.special {

(in jQuery)

$( 'p.special' );

If, alternatively, you had a particular container on your page, let's say it has the class name "quote-of-the-day", and you wanted to style the paragraph tags inside that container to stand out from the rest of the paragraphs on your page, you would write:

(in CSS)

/* this first targets the "quote-of-the-day" container, then more specifically, it targets all the p tags contained therein */
.quote-of-the-day p {

(in jQuery)

$( '.quote-of-the-day p' );

Notice that the CSS and jQuery selectors are all the exact same. I hope this helps to clarify!


Thanks for breaking it down, Erik. Looks like I need to review the CSS courses!

The examples you provided are very thorough. I appreciate it!


Just to simplify Eriks' answer.

p.example or $("p.example") would look like this in html

<div class="wrapper">
  <p class="example"> Lorem Ipsum </p> /*WOULD GET TARGETED*/
  <p>Mor Lorem Ipsum</p> /*WOULD NOT GET TARGETED*/

while .example p or $(".example p") would look something like this.

<div class="wrapper">
  <div class="example">
    <p> Lorem Ipsum </p> /*WOULD GET TARGETED*/
    <p>Mor Lorem Ipsum</p> /*WOULD ALSO GET TARGETED*/

As you can see the elements you target would be completely different.

Hey, Chyno.

Thanks for providing the HTML examples. My understanding of jQuery (and CSS?) is obviously pretty limited. Seeing what the selectors are doing on the HTML side helps a lot.