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 trialAllison Littman
2,325 PointsHow 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
231,248 PointsThe 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
8,932 PointsYou 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:-
- jQuery Basics
- JavaScript and the DOM
- DOM scripting with example
Steven Parker
231,248 PointsClose, except that "li" elements are list items, but the challenge is asking for links.
Sahil Kapoor
8,932 Pointsoh I just read the question and thought that the question is to select the list items with in <nav> elements.