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

Javier Briones
Javier Briones
Front End Web Development Techdegree Student 29,674 Points

How to select child elements from a parent element? example: select all the links (<a>) from the <nav> element.

Challenge is asking to select, using DOM (document.getElementsByTagName), all the links (<a>) inside the <nav> element. I tried using "nav > a" and "nav ul li a"

1 Answer

If you follow the links in the challenge hint, it is suggested to use querySelectorAll() and a descendant selector. From the teacher's notes:

Examples of descendant selectors To create a descendant selector, weโ€™ll need to use two or more selectors separated by whitespace:

.main-header span {
  font-size: 26px;

ul li {
  margin-bottom: 12px;

What you tried with "nav ul li a" will work with querySelectorAll() and can be shortened to just "nav a"