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
Ashley Delph
Courses Plus Student 2,424 Pointsjquery 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
Treehouse Guest TeacherYou're close.
Imagine the HTML looking like this:
<ul class="nav">
<li></li>
</ul>
Let me know if you need any more pointers.
James Barnett
39,199 Points[Edit:] Looks like Andrew responded while I was typing my response, oops
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?
Yuvraj Jatania
2,731 PointsThanks 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
Christiaan Gieles
Front End Web Development Techdegree Student 3,835 PointsWould'nt it be either:
$(".nav + li");
or just simply:
$(".nav li");
Not that I've passed any jquery code before, so just guessing :)
Yuvraj Jatania
2,731 PointsChristiaan - 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' "
Christiaan Gieles
Front End Web Development Techdegree Student 3,835 PointsEDIT: 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")
Yuvraj Jatania
2,731 PointsThat's great! It worked! Many thanks for the help, much appreciated.
Andrew Chalkley
Treehouse Guest TeacherI 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 :)
Ashley Delph
Courses Plus Student 2,424 PointsThanks 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.
Christiaan Gieles
Front End Web Development Techdegree Student 3,835 PointsNo problem :) And I guess Andrews was more general :)
Aaron Timmons
7,824 Pointsthanks Andrew your last example you gave is spot on, i understand it perfectly now!
thans man!!
Joseph Knoll
20,481 Pointswhy 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
Treehouse Guest TeacherHi 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