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 JavaScript and the DOM (Retiring) Getting a Handle on the DOM Practice Selecting Elements

Allison Littman
Allison Littman
2,325 Points

How do you retrieve a list within a navigation with only one variable declaration?

I went through the entire tutorial regarding different ways of selecting items from the HTML but none of them went specifically over this. How did you approach it? What other tutorials can you recommend?

2 Answers

Steven Parker
Steven Parker
229,744 Points

The return of a list vs. a single element is based on the method used. For example, document.querySelector returns one single element, but document.querySelectorAll returns an HTMLcollection, which is essentially a list of elements.

The latter when used with a descendant selector ("nav a") is a good solution for task 1 of the challenge.

Sahil Kapoor
Sahil Kapoor
8,932 Points

You can use " document.querySelectorAll("nav li") ", where the value "nav li" is called the descendent selector which means that select all <li> element that is inside <nav> element; You can also use it with other HTML elements like "nav a" meaning select all anchor (<a>) element that is inside <nav> elements.

And for selecting elements in HTML use can watch:-

  1. jQuery Basics
  2. JavaScript and the DOM
  3. DOM scripting with example
Steven Parker
Steven Parker
229,744 Points

Close, except that "li" elements are list items, but the challenge is asking for links.

Sahil Kapoor
Sahil Kapoor
8,932 Points

oh I just read the question and thought that the question is to select the list items with in <nav> elements.