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 trialAndres Giraldo
7,308 PointsI didnยดt seem to get it right
This is the answer I have for the code challenge, but It didnt work. I get the "Did you use the children property?".
Answer : var listItems = navigation.querySelector("li");
For what I understood, the querySelector function returns the children element that matches the criteria on the parenthesis (in this case, being a <li> element).
Any thoughts on this one?
Thanks in advance for the help :)
Andres
//Select the naviagation
var navigation = document.getElementById("navigation");
//Select all listItems from the navigation
var listItems = navigation.querySelector("li");
//When a navigation link is pressed
var linkListener = function() {
console.log("Listener is clicked!");
}
var bindEventsToLinks = function(listItem) {
//Select the anchor
var anchor = listItem;
//Bind the linkListener to the anchor element (a)
anchor.onclick = linkListener;
}
for(var i = 0; i < listItems.children.length ; i++) {
bindEventsToLinks(listItems.children[i]);
}
<!DOCTYPE html>
<html>
<head></head>
<body>
<ul id="navigation">
<li>
<a href="#home">Home</a>
</li>
<li>
<a href="#about">About</a>
</li>
<li>
<a href="#contact">Contact</a>
</li>
</ul>
<p>A few of my favourite things:</p>
<ul>
<li>
Rain drops on roses
</li>
<li>
Whiskers on kittens
</li>
<li>
Brown paper packages wrapped up with string
</li>
</ul>
<script src="app.js"></script>
</body>
</html>
3 Answers
Tim Holt
3,409 PointsHey Andres,
The question states that line 5 of app.js is wrong, but it looks like you've changed line 19 as well, which may have caused you some confusion.
I was able to get this to work by using querySelectorAll
, so my line 5 becomes:
var listItems = navigation.querySelectorAll("li");
querySelector
will only return the first element found, while querySelectorAll
will return all elements that match the selector. See this article for more info.
I hope this helps!
danditomaso
3,192 PointsHi Andres, I saw that you have a mixture of variables throughout your code, that are named similarity, can you review all the references of "listItems" and "listItem", I see there are both being used in your code and I believe if you remove the "s" on listItem throughout your code block you may have better success.
Andres Giraldo
7,308 PointsThat made the trick!
And it makes perfect sense.
Thanks so much Tim!
Have a nice one :)