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
Monika Wolff
Courses Plus Student 1,746 PointsIntro 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
James Barnett
39,199 PointsThe 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?
JD Richards
4,601 PointsYou are such a teacher @James. (compliment). I was about to write the answer but that's no help is it?
James Barnett
39,199 Points@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.
Andrew Chalkley
Treehouse Guest TeacherThanks for chiming in James :)
Monika Wolff
Courses Plus Student 1,746 PointsThanks 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
Treehouse Guest TeacherHi 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
James Barnett
39,199 Points@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.
David Woody
8,671 PointsI was having the same issue, and this explanation made it very clear. The visualization of the HTML help a ton. Thanks so much!
Monika Wolff
Courses Plus Student 1,746 PointsI got it! Thank you.
Andrew Chalkley
Treehouse Guest TeacherGreat Job Monika!
Kielan Lemons
2,352 PointsI 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.
Michael Blair
Courses Plus Student 5,515 Pointsso why do i have to put the ul before .nav but nothing before #container?
James Barnett
39,199 PointsMichael 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
Aimee Ault
29,193 PointsI 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
Treehouse Guest TeacherWhat does the failure say?
Have you keep the previous line too?
Aimee Ault
29,193 PointsThe 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
Treehouse Guest TeacherIt works for me?
Can you spot any differences?
Aimee Ault
29,193 PointsHeh, 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 :)
Andrew Chalkley
Treehouse Guest TeacherNo worries. I TL;DR all the time ;)
