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 trialchasethurmond3
373 PointsSelect 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
Full Stack JavaScript Techdegree Graduate 20,177 PointsI 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.
Josh Curtis
7,782 PointsHi 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
9,828 PointsHello 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
9,828 PointsHello 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
6,432 PointsWhere does the 'a' in ('nav a') come from? Why wouldn't it be 'li' to represent the child element?
pacificbee
2,205 PointsFingers crossed that someone answers this question. I'm also stumped on what 'a' comes from and why ?
Andrew Carters
3,698 PointsHi 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.
Clyde Wright
5,348 PointsMy gosh!!!! Thanks Andrew carter. I was about to put my laptop away but when I tried your suggestion, the magic happened.
Christopher Brown
4,843 PointsThank you so much! (But why does it work this way, anyone?)
Madeline Yao
Full Stack JavaScript Techdegree Student 9,611 PointsWhy "const navigationLinks" does not work but "let navigationLinks" does?
Nick Huemmer
Front End Web Development Techdegree Graduate 26,840 PointsThe 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
14,286 Pointslet navigationLinks = document.querySelectorAll('nav a');
Matt Fredericks
5,399 PointsMatt Fredericks
5,399 PointsGood 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?