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

Select all links in nav and assign to navigationLinks trouble

I'm stumped. I'm very new to JS. I taking JS/jQ at school and trying to supplement my learning with Treehouse. I watched about half of the basics videos then jumped into the DOM tutorials since they're closer to what I'm going over in class. I'm terrible at it but I understand loops and the mathematics operators and want to get practice understanding DOM...

Current task:

In the following tasks you'll be required to select various elements on the index.html page. In the app.js file on line 1, select all links in the nav element and assign them to navigationLinks.

let navigationLinks = document.getElementsByTagName('nav'); let galleryLinks; let footerImages;

I've tried nav, ul, li, nothing works and all of that seems to be what was previously covered. I re watched the videos and nothing is jumping out at me. I just want to learn. I understand pretty much everything up to this point so backtracking hasn't really been beneficial. Been stuck all day.

Thanks, Chase

8 Answers

Rod Sanati
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Rod Sanati
Full Stack JavaScript Techdegree Graduate 20,177 Points

I don't know why this question was given after this series of videos, since it's never explained how to access a tag within a tag in a single line of code until later on.

The furthest the video explains is:

var navigationLinks = document.getElementsByTagName('nav')[index]

and doesn't even mention you can access a parent/child node the way Josh explained, with a simple space between the two.

Thanks for the examples, josh/loannis. Both seem to work.

Matt Fredericks
Matt Fredericks
5,399 Points

Good question! "why this question was given after this series of videos, since it's never explained how to access a tag within a tag in a single line of code until later on." Why would they ask you to demonstrate something you haven't learned yet?

Josh Curtis
Josh Curtis
7,782 Points

Hi Chase,

I tried to use an index and for loop like Loannis mentioned and couldn't get it to work. I ended up just selecting nav and then the links:

const navigationLinks = document.querySelectorAll('nav a');
let galleryLinks;
let footerImages;

I'm not sure if this is the best/correct way to do it though. I would appreciate any other solutions!

Ioannis Leontiadis
Ioannis Leontiadis
9,828 Points

Hello Josh,

your code should work just fine. It would be beneficial though to consider the following:

  • parentNode.querySelectorAll() is generally slower than parentNode.getElementsByTagName()
  • parentNode.querySelectorAll() returns a StaticNodeList

You can read more about these in an article by Ryan Morr.

For simple cases I think there is no concern however.

Can you post what you did? There is no need for a loop in the suggested method.

Ioannis Leontiadis
Ioannis Leontiadis
9,828 Points

Hello Chase,

methods like

parentNode.getElementsByTagName(tagname: string)
parentNode.getElemetsByClassName(className: string)

return a NodeList object which can be thought of as an array of elements, inside the parentNode, with the specified tagname or className and so on. Also note that the nodes of a NodeList object can be accessed by an [index], just like any elements of an array.

So, this code

var navigationLinks = document.getElementsByTagName('nav');

actually stores in navigationLinks all nav tags of the parentNode, in this case the document, as an array.

If you want all the anchors inside a specific nav tag you could use something like

var navigationLinks = document.getElementsByTagName('nav')[index].getElementsByTagName('a');

where you select a nav tag using [index] and then retrieve all of it's anchor children as an array.

Tip: Try using the console to get familiar with traversing the Document Object Model.

Hope that helped!

Daniel Grieve
Daniel Grieve
6,432 Points

Where does the 'a' in ('nav a') come from? Why wouldn't it be 'li' to represent the child element?

Fingers crossed that someone answers this question. I'm also stumped on what 'a' comes from and why ?

Andrew Carters
Andrew Carters
3,698 Points

Hi all, pretty confused myself on this one. Tried many things.

const navigationLinks = document.querySelectorAll('nav a');

i thought was the correct answer, but would not work at all. However, remove the semi-colon at the end -

const navigationLinks = document.querySelectorAll('nav a')

and the code editor challenge is happy with this.

My gosh!!!! Thanks Andrew carter. I was about to put my laptop away but when I tried your suggestion, the magic happened.

Thank you so much! (But why does it work this way, anyone?)

Nick Huemmer
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Nick Huemmer
Front End Web Development Techdegree Graduate 26,840 Points

The solution to this part of the problem is not explained anywhere in the material and requires research to get it done, which basically means coming to this thread.

Why does

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

work? It's nice to have the answer, but would be even nicer to know the why so we can learn from this rather than just use our research skills.

Abhishek Shah
Abhishek Shah
14,286 Points

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