JavaScript JavaScript and the DOM 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"