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

Return Elements Using CSS Selectors

https://teamtreehouse.com/library/javascript-and-the-dom-3/getting-a-handle-on-the-dom/return-elements-using-css-selectors

I keep getting error messages no matter what approach I use to answer the first of these objectives in the link above.

Here's the question:

"In this challenge, you're going to select various elements from index.html. First, select all <a> elements inside the <nav> element and assign them to the variable navigationLinks. (HINT: Use a descendant selector to match elements that are descendants of an element.) "

After many different attempts, this is the closest I can get:

let navigationLinks = document.querySelectorAll('nav');

Here's the error message I get:

"Bummer: Make sure that you're selecting 3 links. It looks like you've selected 1 links."

What am I doing wrong?

1 Answer

Steven Parker
Steven Parker
231,007 Points

You haven't actually selected any links, but the response is correct that you selected just one element.

The instructions ask you to "select all <a> elements inside the <nav> element", but what that code does is to select all <nav> elements (and there is only one). To select elements that are inside another element you can use a descendant selector, which is the container selector combined with the target selector using a space. So to select all <a> elements inside the <nav> your selector would be "nav a".

That worked! Thank you for your help.