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

Chlallenge 2/2 "JQuery to select elements"

can you post the actual challenge question? cant really help you without knowing what its asking for

6 Answers

Mariana,

The task is "On the next line, use jQuery to select all list items (li) in an unordered list (ul) with the class of 'nav'?" But your selector

$(".nav ul");

would select all unordered lists that are descendants of any element with the class "nav". Change your selector to have the unordered list with class on the left and the list item on the right.

Thanks Chris, something like that? $("ul nav, li"); I really don't understand what I have to do...

Close! The comma breaks the selector up into 2 different selectors. So

$("ul nav, li");

actually selects "ul nav" AND "li" separately and combines them. So let's break down the task. "Select...

/* all list items (li) */
li

in

/* an unordered list (ul) with the class of 'nav' */
ul.nav

So the resulting selector would be:

$("ul.nav li");

which selects all "li" elements that are decendants of (or inside of) a "ul" with the class of "nav".

If you find yourself not understanding the selector syntax for jQuery then you'll want to refresh your CSS selector skills. Because jQuery uses CSS selectors! Try http://teamtreehouse.com/library/websites/css-foundations-2/selectors as a refresher.

Great description ;) Thanks!

That is odd...because

$("ul.nav li");

is not a valid css selector....the valid selector being asked for is

$(".nav ul li");

which i tried first and could not for the life of me figure out why it didnt work. It is weird because Andrews video clearly states that ANY valid css selector will work...but that apparently is not the case.

Also, What code editor are you using to embed?

Hi, Timothy Boland,

I'm wondering why you say that

ul.nav li

is not a valid CSS selector? It is a valid selector which means, "Select all li elements which are contained within a ul that has the class of 'nav'." Which is exactly what the code challenge is requesting.

.nav ul li

means, "Select all li elements which are contained within a ul element which is contained within any element with the class of 'nav'." That is also a valid selector but it is not what the code challenge is looking for.

I'm pretty sure we are using CodeMirror for the embedded syntax highlighting.

Thanks!

I tried ul.nav li as a regular css selector; but it didnt work - .nav ul li did work as expected

<body>
    <div class="nav">
        <ul>
            <li>List Item One</li>
            <li>List Item Two</li>
            <li>List Item Three</li>
        </ul>
    </div>
</body>
/*
.nav ul li {
    color: red;
}
*/

ul.nav li {
    color: red;
}

Ah, yes, Timothy Boland. Given the HTML you're working with ".nav ul li" would work where "ul.nav li" wouldn't. But this question was about the code challenge at http://teamtreehouse.com/library/build-an-interactive-website/introduction-to-jquery/using-jquery-to-select-elements

For that code challenge, in step 2, it asks specifically, "On the next line, use jQuery to select all list items (li) in an unordered list (ul) with the class of 'nav'?" For this code challenge ".nav ul li" is not correct. This challenge would use HTML more like:

<body>
    <ul class="nav">
        <li>List Item One</li>
        <li>List Item Two</li>
        <li>List Item Three</li>
    </ul>
</body>