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

Richard Moy
Richard Moy
1,271 Points

JavaScript and the DOM: I can't seem to correctly select all the proper links on the HTML page. What is correct method?

I'm supposed to set a variable equal to all the links on the HTML page. I used the getElementsByTagName method but it doesn't seem to work. What is the proper way to select the relevant links?

1 Answer

Mike Hickman
Mike Hickman
19,817 Points

Hi Richard,

For that one, I'd try by using .querySelectorAll, since you want to be specific and choose the ones within the nav element. getElementsByTagName('a') would get all the 'a' elements, but it only wants you to get the ones within <nav>.

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

This is saying "Hey! Give me all of the 'a' elements that are within 'nav'". There might be a million other ways to do this, but that's how I'd go about it.

Have fun,

Mike

Richard Moy
Richard Moy
1,271 Points

Thanks Mike. That was perfect!

Mike Hickman
Mike Hickman
19,817 Points

Great! Have a good one.