JavaScript JavaScript and the DOM Getting a Handle on the DOM Practice Selecting Elements

Scott Shanley
Scott Shanley
4,309 Points

Looking for some help here. Tried everything, and can't move on without an answer...

Not sure if I'm just confused by the wording here or if it's something really simple i'm just overlooking. Super frustrating since I can't just see the answer after getting it wrong a thousand times.

Michael Hulet
Michael Hulet
Treehouse Moderator 46,564 Points

Could you post the code you've written so far here? It makes it a bit easier for us to help you out 🙂

Scott Shanley
Scott Shanley
4,309 Points

Been playing around with a bunch of different variations of the below.

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

1 Answer

Michael Hulet
MOD
Michael Hulet
Treehouse Moderator 46,564 Points

It looks like you actually have the basic gist of this, but I see 2 issues:

  1. You have the right idea that the easiest thing to do here would be to reference the elements by a CSS selector, but the getElementsByTagName function only takes the name of a single kind of tag and returns an array of all elements of that tag in the page. In order to query the DOM using a CSS selector, you need to use the querySelectorAll function, which takes a CSS selector as an argument and returns an array of all elements matching that selector
  2. You're selecting the wrong elements with your CSS selector. This step of the challenge asks you to select all links within the nav tag, but your selector targets all list items in an unordered list in the nav tag. I know it's fairly counterintuitive, but in HTML, a link is actually represented by the a tag

Once you take care of those 2 issues, I think you'll be on the right track for this challenge. Great job!

Scott Shanley
Scott Shanley
4,309 Points

Got it! Thank you for your help and quick response!