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

How do I select the href links in the NAV html element, without selecting all href on the page.

Basic question, but need help Lol. I stopped DOM coarse for while, started back yesterday and was in middle of coarse. I feel like i'm good at selecting elements and manipulating them for my experience but don't know the syntax of selecting just the href links in the NAV element. I know I can add ID or a Class to each link in NAV element, but I don't feel like that is what Treehouse is asking and want to do it right to build my skills. I know CSS good, but don't know the CSS pseudo-class selectors by heart. Is this why I can't figure it out? If so ill brush up on these if ill need them more in JavaScript selecting elements.

js/app.js
let navigationLinks = document.querySelectorAll('nav:href');
let galleryLinks;
let footerImages;
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>

10 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi there! I feel like this tip will get you there. Remember, that a link is denoted by the anchor tag or <a> tag. The href is an attribute. You don't need to select by the attribute, but rather the element.

For example, to select all h1 elements inside div elements you could do querySelectorAll('div h1'); This would select all h1 elements that reside inside a div element.

Hope this helps! :sparkles:

Rob Allessi
Rob Allessi
8,600 Points

Michael Slattery

Nothing has been deleted by Treehouse staff unless the post itself was off topic and violated our TOS (abusive language, racist or sexist remarks, promoting or marketing, etc.).

No one is being deceptive in this thread nor have we been deceptive in our courses. That provides no benefit to us nor to our students. We ask that you treat other mods and students with kindness and respect. For any further conversation on this topic, please email us at help@teamtreehouse.com.

Thank you Jennifer, passed that part of coarse now, good deal.

What happened to the Questions on this subject being answered by Idilate? If he thinks that everything we needed to answer this question was given . He will need to show us where the syntax ('nav a') was in the lesson and why it took him three trys at answering the problem before he came up with it. I suggest he take notes and put he students learning the material before feeding his own ego

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi! I'm not sure what you're referring to. I haven't seen anyone named "Idilate" post an answer on this question. Also note that most of us who answer here are students just like yourself. This particular answer was given by myself over 3 months ago and it didn't take me 3 tries (if you're referring to me).

I am referring to a very similar line of questioning that was referring selecting list items from under the tag nav. The assumed author did not take kindly to to the very justified criticism that the question was beyond the scope of the instruction given. I was not the only person with this problem as it was a problem for someone years ago and was still a problem today. Still not addressed by Treehouse. Appearently there was enough embarrassment that Treehouse has now removed the community question,answers, and comments much as how they handle things in Russia. I see where you came up with an analogous answer to his which wasn't his first stab at it. This syntax was not mentioned in the lesson.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I believe you might be referring to this student: I Dilate. To be clear, students (including both you and myself) may choose to delete answers and comments at any time they like. I have never known Treehouse staff to delete an answer or comment unless it was specifically abusive. Remember, when you post here you are addressing other students and not Treehouse staff. In my experience Treehouse staff loves to get student feedback. If you have suggestions, you should send them to Support at help@teamtreehouse.com.

To be perfectly clear, neither I Dilate nor myself are Treehouse staff.

Also, a student may choose to delete an question that they have posted or have it removed by staff. If they choose this, all comments and answers disappear along with the post.

edited for clarification

Students may choose to edit or delete any answers or comments which they themselves have posted.

I Dilate
I Dilate
3,983 Points

Michael is referring to another (similar) question that was asked by another user. This thread

Treehouse haven't deleted anything on that thread, but it's probably less confusing for people that ask questions if we avoid spilling this conversation over into here too.

I think he's annoyed because I felt that the question was clear enough to answer the challenge (as Jennifer said - I'm just a student here like you. I managed to answer it myself after taking the lesson very recently), but I think he's saying that there isn't enough information provided in the lessons to be able to answer the question.

He also appears to be frustrated that I edited my answer to that question a couple of times to try to make it as clear as possible.

I too am a stutent. You seem to know alot about Treehouse policy and are obviously willing to stand up for them. Are you saying that one member can delete anothers comments? A whole line of questions has gone missing. What is your relationship to this organization? Funny that you should know that I Dilate is a student. Nice work on that syntax. I wish I had see it prior to being tested on it. Deceiving is also a form of abuse. How would you know if I Dilate is on staff?

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I'm saying that comments are attached to either questions or answers. When the Question or Answer that the comment is attached to is deleted, so does the comment. As far as knowing I Dilate is a student, it's not hard to figure out. You can look at his profile here: https://teamtreehouse.com/dilate. Staff are clearly marked with a "Staff" across their Treehouse profile while moderators are marked with "Mod", much as your account is marked with "Pro".

Also, as a moderator, I do have some contact with Staff specifically regarding the community. I have been learning at Treehouse since February 1, 2016 and I don't believe anyone is trying to deceive anyone else. Yes, having answered over 3300 questions here on the forums, I do believe I have some experience with this platform. If you believe something immoral or deceptive has happened here I highly encourage you to contact staff at the email I mentioned. :smiley:

I Dilate
I Dilate
3,983 Points

Michael, I posted a link to the thread you're talking about 20 minutes ago.

Nobody is trying to deceive you. What would be the point?

I can assure you that I have nothing to do with Treehouse - I'm just studying to code, just like you. I have no special knowledge about this lesson - hence I thought there was enough info to answer it because I was given just the same to work with as you.

All you're doing is making it really hard for the poster of this thread to stay focused on the help they need to understand the solution to the challenge.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Michael Slattery just as a case in point there are questions and answers out there on the forums right now and have been there a while which definitely give a negative impression of course materials/teachers/staff and more.

I offer this as a case in point: https://teamtreehouse.com/community/it-easier-to-fall-into-a-gorilla-pit-then-to-get-help-on-here-do-the-so-call-instructor-even-check-these-questions

And yet, this has stood here for quite some time and was never removed by Staff.

Jennifer, By your own discription. to pass yourself off as not on staff at Treehouse is just shy of telling the whole truth. The word deceit is defined in any English language dictionary. I Dilate: you both are far better coders than I. The point is that the lesson is trying to educate someone on my level and didn't give enough info. Certainly HTML is selected thousands of times a day using this syntax. Is it supposed to be a secret? Am I supposed to find these answers only because many others couldn't figure out the text or video either and teach myself through your forum? This and many other issues are not being taken care of even after years. Now if you will kindly show me in the lesson where this syntax is discussed and I failed to take good enough notes as to your specifications, I will apologize. else this would be a great place for your appology. Clearly you have a newer definition of the word staff than mine. Do you make money there? Other than your vast amount of programing knowledge and experience we were given the same to work with.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

Hi Michael Slattery! I'm not sure how it's not exactly the whole truth. I'm not employed by Treehouse nor do I make money from them. I actually work as an IT-manager for a small school in Sweden. I belong to a volunteer team that helps answer questions on the forum, edit markdown for those who need it, and report bugs/abusive behavior to Treehouse Staff. I began here with rather little knowledge of programming. I'd had some exposure to it as a child, but not much more. You're welcome to read the interview that Treehouse did with me a while ago: here. I began at almost 0 just like you are.

As to the syntax question, no... the syntax I used here to pass the challenge was not in the video. Nor did I ever state that it was. My guess is that the other thread was attempting to use the syntax presented in the video, but this was not the case in this question. The original poster of the question was trying to use a different syntax (which is also perfectly valid and you will learn about later). I posted code that would allow them to pass the challenge given the syntax they were attempting to use.

To pass this challenge using the syntax presented in the video, I would have done something like this:

let nav = document.getElementsByTagName('nav')[0];
let navigationLinks = nav.getElementsByTagName('a');

The first part selects the navigation element and stores it into a temporary variable that I chose to name nav. The second line then gets the anchor elements inside that nav and assigns the result to navigationLinks.

An example analogous to the first line is shown around 3:12 of this video although they are selecting an h1 instead of a nav.

At 5:19 of the same video you can see an example similar to the second line although they are selecting li instead of a.

Now, the difference here is that in the second line instead of selecting from the document we are selecting from the selection we already made. This is perfectly valid and is documented here.

As specified in the documentation, it takes this form: elements = element.getElementsByTagName(tagName)

Just like the body, nav, p are elements... so is the document.

This documentation is linked in the Teacher's Notes for easy access.

I hope this clears up some misunderstandings. I certainly was never out to intentionally deceive anyone. :sparkles:

edited for update

After reviewing the course material again, I do find that querySelector and querySelectorAll were both introduced in this video. The video in question ends by saying this:

As you can see these selection methods are very powerful and flexible and there's a lot more they can do that we didn't have time to cover. But I'm including the resources in the teachers notes you can view to learn more about them.

In my opinion, to get past the first step you had two choices: extrapolate the information from the first video I linked and use that to solve the problem, or read further in the documentation as advised by the instructor in the second video. Either should have lead you to a solution.

Michael Hulet
Michael Hulet
47,912 Points

Hey, I’m another unpaid, remote-working, volunteer moderator, but a far less patient one than Jennifer. This is a pretty clear-cut case of harassment, and I’m going to treat any further comments as such. Stick to code, and don’t make accusations. If you have any non-technical questions, please direct them to either me or support, though they likely won't be back in their offices until Tuesday

I Dilate
I Dilate
3,983 Points

Michael,

I agree there is some confusion in this lesson, because clearly several people are asking the same question, so perhaps the guys at Treehouse could add something to the Teachers Notes to give a hint for anyone that is stuck.

I think the key learning here is what goes into querySelectorAll();

querySelectorAll() searches for a CSS selector (this is clearly stated in the lesson it's introduced).

If you check the documentation for querySelectorAll() on MDN (something that was advised in several videos too) you'll also see clearly stated that querySelectorAll() accepts CSS selectors.

If you have taken the HTML+CSS classes, or you have any experience in CSS, then you'll know that you can string multiple selectors together to do different things, for example:

To select all of the <h1> tags that reside inside a <header> using CSS we can write a rule like this:

header h1 {
   font-size: 3em;
}

Or (in the case required for this challenge), imagine how to select all of the <a> links inside the <nav> using CSS:

nav a {
   color: red;
}

So that gave us a CSS selector of "nav a" - which is exactly what you put into querySelector.

querySelectorAll() works in the exactly the same way you normally work with CSS. There is no special JavaScript notation for it - you just put a CSS selector into the brackets. I think perhaps that's the problem, people are expecting a different, javascript specific notation for it - but there isn't one.

W3Schools publish some good examples of the various types of CSS selector running inside querySelectorAll() if you'd like to see some.

I suspect that it must be difficult for the guys at Treehouse to determine how much information is the right amount with which to put in a video... For example, should they also need to teach everyone what a <tag> is within the JavaScript course, because they may not know what HTML is yet? There are some prerequisites to learning JS, and with using querySelectorAll() the prerequisite is a basic understanding of CSS selectors.

I think Treehouse strike a good balance generally, but as I said, perhaps they can do more in this case to clear it up for people that are struggling.

That said - I don't think its hard to find a million examples of how querySelectorAll() works using Google, so part of the responsibility for learning must also lie with the student.

Lastly - I wasn't trying to be condescending when I wrote my comment about taking notes, although you seem quite offended by it (I apologise for causing any offence). The original poster in the thread yesterday was trying to use querySelector() rather than querySelectorAll() in the first instance - which is something that was covered quite clearly in the video, but is easily forgotten. I was merely saying that when I took this lesson (and any lesson in fact), I feel that writing as many notes as possible really helps me to remember the sometimes confusing, and easily forgotten syntax.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I Dilate I believe a lot of the confusion was that the original poster of this particular question was using the querySelector which is not presented at all in the videos. The use of querySelector won't come until much later. But somehow this particular student had already been aware of it and was trying to use it. The hints I gave were to allow them to pass using that method instead of the ones in the video, which is perfectly valid and reasonable.

edited for update

I do find that the querySelector and querySelectorAll were introduced in this video. That being said, I cannot find an instance where they used it to pick an element inside another element and that would have required following the instructors advice and reading the documentation on more things that are possible using this method. I apologize. Having taken so many courses here things can get a bit jumbled. For some reason, I truly believed the querySelector and querySelectorAll were first visited in another course.

I Dilate
I Dilate
3,983 Points

I hadn't seen Jennifer's response before posting. But it does raise another issue - there are multiple ways to solve this challenge, and you don't need to do it all on one line either. The route she suggests will work just fine too.

I Dilate
I Dilate
3,983 Points

Jennifer Nordell Yeah that makes sense. When people answer a question in Community, it can difficult to know what exact syntax has been taught up to that point. I had imagined that querySelector() and querySelectorAll() were covered by that point - if they aren't then your answer is more appropriate to that stage of learning.

When you start using querySelector() + querySelectorAll() a lot of people like to stick with it, rather than diving back into getElementsByTagName etc, etc, so I think it may skew advice toward a certain syntax too.

I Dilate
I Dilate
3,983 Points

Jennifer Nordell Looks to me that querySelector() and querySelectorAll() are dealt with in some detail just 4x steps prior to this challenge, in... "Using CSS queries to select page elements" and it goes into detail of using nth:child in fact, which is actually more complicated than the basic CSS selector of "nav a" I was discussing.

Jennifer Nordell
seal-mask
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

I Dilate Yes, you'll notice that I updated both comments that I left to reflect that the querySelector and querySelectorAll was covered. That being said, I can not find any place where they select an element within an element. That might be obvious for people who know CSS, but for people without that knowledge it would likely have required reading the MDN documentation listed in the Teacher's Notes.

Seems you only accept posts that don't point out your company's shortcomings.such as disagreeing with your employees who were clearly wrong and making condescending remarks. read the entire transcript. At no time did I say anything that was untrue or unfair or abusive. In fact your group owes me an apology and admission of deceptive practices and an admission of being wrong about a topic they had 3 days to support. Read your own peoples comments. The topic in question was not covered in the lesson.

Michael Hulet
Michael Hulet
47,912 Points

As clearly stated by Jennifer before me, and myself since then, nobody in this thread works for Treehouse. If you'd like to ask real staff about that, you can email support. I haven't seen anyone here (except you in your post I had to delete, as well as a few others) be condescending, either. Please refer to Jennifer's brilliant explanation about your code questions, and where in the lessons they were included. Documentation is most definitely within the scope of any lesson on Treehouse. In a school context, where a Treehouse video is the lesson, the documentation is the textbook. When you're actually writing real code, you'll need to know and understand the documentation very well. Teachers often link you to it so you understand it better and know how to solve the problems you run into. That is a necessity for any coding job or hobby. Treehouse is a school, so it would be remiss to not familiarize you with the single most important resource you have in the trade it teaches.

Also, for the record, I've edited a word in your comment here. You've inadvertently stumbled into a lewd phrasing, though it clearly wasn't intended. Regardless, I've changed it to the spelling you were going for ("shortcomings")