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 trialAndrea Sorrell
4,736 PointsNeed hint or answer to challenge question.
Take a look around the html and js files. Line 5 of app.js is incorrect. listItems should be the children of the navigation unordered list.
This is what I have for line 5
var listItems = navigation.querySelector("li");
I am unsure of the right way to denote selector in parenthesis. When I submit I am asked "Did you use the .children
property?
11 Answers
Hugo Paz
15,622 Pointsvar listItems = navigation.children;
Rene Katsev
Courses Plus Student 3,621 PointsHey Blake u are missing the quotes around the "a" should be ->
var anchor = listItem.querySelector("a");
Zeljko Maric
8,524 Pointsvar anchor = listItem.querySelector("a");
doesn't work. Any ideas why? It sends me back to task 1 and gives the following error:
There was an error with your code: TypeError: 'undefined' is not a function (evaluating 'listItems.querySelector("a")')
Hugo Paz
15,622 PointsHi Zeljko,
I tried
var anchor = listItem.querySelector("a");
and it worked.
From your error message it is evaluating listItems instead of listItem, maybe a typo?
Zeljko Maric
8,524 PointsYes, now it worked, Hugo! Thanks!
imtrying
19,943 PointsIn case someone else need the answer make sure you add a space after the quote::
like this (" a ");
MUZ140875 Joseph Chivasa
7,758 PointsThis is wat i did ........var listItems=navigation.children; n i got it right
Prashant chaudhari
Courses Plus Student 24,499 Pointscorrect code.
var navigation = document.getElementById("navigation");
//Select all listItems from the navigation var listItems = navigation.children;
//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.length ; i++) { bindEventsToLinks(listItems[i]); }
Tanyaradzwa Anesu Mukondiwa
18,350 Pointsar anchor = listItem.querySelector("a");
Ishan Vyas
30,284 Pointsvar anchor = listItem.querySelector("a");
Larry Singleton
18,946 PointsThis confused the heck out of me, this is what worked for me. //Select the naviagation var navigation = document.getElementById("navigation");
//Select all listItems from the navigation var listItems = navigation.children;
//When a navigation link is pressed var linkListener = function() { console.log("Listener is clicked!"); }
var bindEventsToLinks = function(listItem) { //Select the anchor var anchor = listItem.querySelector("a"); //Bind the linkListener to the anchor element (a) anchor.onclick = linkListener; }
for(var i = 0; i < listItems.length ; i++) { bindEventsToLinks(listItems[i]); }
Blake Jackovitch
6,688 PointsI'm having a bit of trouble on this as well, unsure as to what is desired here.
michaelmclaughlin3
Courses Plus Student 5,092 PointsMake sure you add '' around the a element
Andrea Sorrell
4,736 PointsAndrea Sorrell
4,736 PointsThanks Hugo. Weird I don't remember going over .children in the video. It worked but it seems to break when I move onto challenge 2.
Challenge task 2 of 2 ask:
On line 14 of the app.js, we want to get the anchor (the a element) from inside the listItem. Use a method to traverse and select the anchor.
It says task one no longer works when I add .querySelector(a) to line 14. any idea why?
var anchor = listItems.querySelector(a);
this is the everything from challenge
Hugo Paz
15,622 PointsHugo Paz
15,622 PointsTry var anchor = listItems.querySelector("a");