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

General Discussion

Intro to jQuery- code challenge help

The question asks:

On the next line, use jQuery to select all list items (li) in an unordered list (ul) with the class of 'nav'?

This is what I typed, what did I do wrong (the first line is the first part of the code challenge, that was correct)?

$("#container"); $(".nav ul li");

14 Answers

The key to this question is understand that the <ul> element has a class of nav, in other words it's <ul class = "nav">.

So the HTML would look something like this:

<ul class="nav">
  <li></li>
</ul>

Now ask youself ... what is the CSS selector I would use to select all <li>s from the above markup?

You are such a teacher @James. (compliment). I was about to write the answer but that's no help is it?

@JD - Thanks.

My view on it is, that generally speaking knowing the answer without understanding it is not much help.

This is because in programming (like math), one thing builds on another.

So if you don't learn how something works you'll have a hole in your understanding. Until you fill those blanks you will continue to be confused whenever that particular concept comes up.

Thanks James,

I feel really stupid, I still don't understand. Am I correct that .nav is the correct code to select everything that has a class of nav? I thought if I typed .nav ul li I would be selecting all <li> and <ul> with the class of nav.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Moninka,

.nav is the correct class name but the class is applied to theul element, not it's parent. Because classes can be applied to more than one type of element you need to be specific.

Given the following HTML:

<div class="error">
   <p>There is something wrong</p>
</div>

The CSS selector of .error div p wouldn't select the paragraph above but div.error p would. Does that make sense?

If you're still struggling with CSS have a look at the CSS Foundations course

Regards Andrew

@Monika -

Here's the markup you are selecting against:

<ul class="nav">
  <li></li>
</ul>

Your selector is .nav ul li

What that translates to is ...

  • Find any elements with a class of nav
  • look inside find any uls
  • then look inside and find any lis.

However the problem with that CSS is there is no ul inside of the element with a class of nav, they are one and the same. The ul is the element with the class of nav.

I was having the same issue, and this explanation made it very clear. The visualization of the HTML help a ton. Thanks so much!

I got it! Thank you.

I had trouble with this as well. That last explanation was very good from james. it would be nice to be able to see the CSS in quiz.

so why do i have to put the ul before .nav but nothing before #container?

Michael Blair - What you are doing there is combing class & type selectors.

You can read more about how that works here: http://css.maxdesign.com.au/selectutorial/selectors_class.htm#combining

I hate to reopen an old issue but is this question still working correctly?

I mean, I submitted:

$('ul.nav li');

and that fails. I've tried every variation possible, given that the question is written so ambiguously, and tried selecting through both .children and .find... nothing passes.

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

What does the failure say?

Have you keep the previous line too?

The error it gives me is:

Bummer! You need to use '$()' again to select the list items

I kept the line before it from the previous task. The funny thing is, I've apparently passed this code challenge before and didn't notice it (I'm only doing it because I'm a gamification junky, not because I don't already know how to use jQuery :))... so it's only now failing.

Andrew Chalkley
Andrew Chalkley
Treehouse Guest Teacher

It works for me?

Can you spot any differences?

Heh, so I don't know why, but I for some reason misread the first task in the challenge and thought it was asking to select the width of #container... which passed. It caused the second task to fail. I'll chalk this up to my own inability to read :)