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 first code quiz

I have watched the first two videos and the bonus materiel but on the quiz am stuck on the last question which asks to use jquery to select all lists in an unordered list with the class "nav"

now I have been typing this as the answer = $("li ul .nav");

can anyone show me where am going wrong ?

Thanks

13 Answers

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

You're close.

Imagine the HTML looking like this:

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

Let me know if you need any more pointers.

[Edit:] Looks like Andrew responded while I was typing my response, oops :flushed:


The key to this question is to 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 yourself ... what is the CSS selector I would use to select all <li>s from the above markup?

Thanks for the above pointers but for some reason I'm still having a bit of difficulty with this code challenge. The answer I'm coming up with is:

$(".nav > li");

Any help would be much appreciated

Would'nt it be either:

$(".nav + li");

or just simply:

$(".nav li");

Not that I've passed any jquery code before, so just guessing :)

Christiaan - thanks but unfortunately those lines of code don't work either :(

Keep getting this error message: "Enter the string of the CSS selector for all li elements in a ul element with the class of 'nav' "

EDIT: Excuse me, I'm tired.. I had spare time, so I looked up on the jQuery site for the correct selector. The correct one should be:

$("ul.nav > li")

Source: http://api.jquery.com/child-selector/

That's great! It worked! Many thanks for the help, much appreciated.

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

I could make this more forgiving but imagine a page with :

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

<div class="nav">
  <ol>
      <li>Dont' select this</li>
  </ol>
</div>

So the correct selector is ul.nav li :)

Thanks Andrew for your pointers. Loving this website and the courses so big shout out to all the staff keep up the good work.

Thanks again for everyone's replies.

No problem :) And I guess Andrews was more general :)

thanks Andrew your last example you gave is spot on, i understand it perfectly now!

thans man!!

Joseph Knoll
Joseph Knoll
20,481 Points

why doesn't $(".nav li") work? wouldn't .nav automatically select the ul that has it as the class, and then the li grab all the list items under it? Is the 'ul' before the '.nav' necessary when coding?

Andrew Chalkley
STAFF
Andrew Chalkley
Treehouse Guest Teacher

Hi Joseph,

It depends on the HTML for example the li in the ul and ol would be selected in the following:

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

<div class="nav">
  <ol>
      <li>Dont' select this</li>
  </ol>
</div>

Whereas ul.nav li would only select the li elements in all ul elements with the class of .nav. It's a very specific question for a specific answer. $(".nav li") would work if you didn't have the other HTML present.

So that answer is correct sometimes, whereas ul.nav li is always correct.

Regards Andrew