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

On line 2 of app.js, select all links in the unordered list with the id of "gallery" and assign them to galleryLinks.

let galleryLinks = document.querySelectorAll('#gallery li');

js/app.js
let navigationLinks = document.querySelectorAll('nav a');
let galleryLinks = document.querySelectorAll('#gallery li');
let footerImages;

//#navigation .navigationLevel2 li
index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Nick Pettit | Designer</title>
    <link rel="stylesheet" href="css/normalize.css">
    <link href='http://fonts.googleapis.com/css?family=Changa+One|Open+Sans:400italic,700italic,400,700,800' rel='stylesheet' type='text/css'>
    <link rel="stylesheet" href="css/main.css">
    <link rel="stylesheet" href="css/responsive.css">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
  </head>
  <body>
    <header>
      <a href="index.html" id="logo">
        <h1>Nick Pettit</h1>
        <h2>Designer</h2>
      </a>
      <nav>
        <ul>
          <li><a href="index.html" class="selected">Portfolio</a></li>
          <li><a href="about.html">About</a></li>
          <li><a href="contact.html">Contact</a></li>
        </ul>
      </nav>
    </header>
    <div id="wrapper">
      <section>
        <ul id="gallery">
          <li>
            <a href="img/numbers-01.jpg">
              <img src="img/numbers-01.jpg" alt="">
              <p>Experimentation with color and texture.</p>
            </a>
          </li>
          <li>
            <a href="img/numbers-02.jpg">
              <img src="img/numbers-02.jpg" alt="">
              <p>Playing with blending modes in Photoshop.</p>
            </a>
          </li>
        </ul>
      </section>
      <footer>
        <a href="http://twitter.com/nickrp"><img src="img/twitter-wrap.png" alt="Twitter Logo" class="social-icon"></a>
        <a href="http://facebook.com/nickpettit"><img src="img/facebook-wrap.png" alt="Facebook Logo" class="social-icon"></a>
        <p>&copy; 2016 Nick Pettit.</p>
      </footer>
    </div>
  <script src="js/app.js"></script>
  </body>
</html>

Have a blessed day.

8 Answers

You are selecting the li (list-item) elements, the task wants you to select the a (anchor) elements. Like this:

let galleryLinks = document.querySelectorAll('#gallery a');

Why doesn't

let galleryLinks = document.querySelectorAll('#gallery li a');

work? the quiz told me it selected 3 links instead of 2?

See above.

Thank you and have a blessed day.

li's are list items, but a's are links. So you should select "#gallery a" as opposed to "#gallery li"

Why doesn't

let galleryLinks = document.querySelectorAll('#gallery ul a');

work?

The <ul> here is <ul id="gallery">; #gallery already selects the ul. There is no other <ul> within it so '#gallery ul' won't match anything.

I don't understand why (or didn't realize) you can select multiple things within in the parentheses. I thought it was one of two things e.g. ( 'li') or ('[title = label']). Can someone explain this? Thanks!

Jenn Glas: The thing you put into the parenthesis of the querySelector/querySelectorAll method is a CSS selector. Anything that would count as a valid selector in CSS is accepted by those methods. And there are a lot of different CSS selectors that can be used.

I would recommend you take a look at this article. The article lists the most common and useful CSS selectors that exists. While I disagree with the article's headline suggesting that you have to memorize all of them, I would recommend you at least bookmark that article or something for later reference and study. CSS selectors are very powerful and very useful in JavaScript.

Both of the examples in your post are CSS selectors 'li' is a tag selector and '[title = label'] is an attribute selector. The CSS selector that allows you to select nested elements that was shown in the answers above is a descendant selector.

The correct answer is let galleryLinks = document.querySelectorAll('section a ');

let galleryLinks = document.querySelectorAll("#gallery a");

this works let galleryLinks = document.querySelectorAll("#gallery li a");

Jermaine Witter
Jermaine Witter
33,087 Points

let galleryLinks = document.querySelectorAll('.social-icon'); This also works because each of the images in the footer have a class of social-icon.

Is the hashtag there because its an ID?

yes:)