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 Interactive Web Pages with JavaScript Traversing and Manipulating the DOM with JavaScript Traversing Elements

Traversing and Manipulating the DOM

I am struggling with this challenge or I don't know what to do exactly. The question is take a look around the html and js files. ListItems should be the children of the navigation unordered list. Fix it.

app.js
//Select the naviagation
var navigation = document.getElementById("navigation");

//Select all listItems from the navigation
var listItems = navigation.querySelector("ul");

//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]);
}
index.html
<!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>

4 Answers

Patrick Koch
Patrick Koch
40,498 Points

as I see you need all children of the list by

var listItems = navigation.children;

The ParentNode.children read-only property returns a live HTMLCollection of child elements of the given object.

The items in the returned collection are objects and not strings. To get data from those node objects, you must use their properties (e.g. elementNodeReference.children[1].nodeName to get the name, etc.).

  • by mdn

g patrick

Jeremy Faith
PLUS
Jeremy Faith
Courses Plus Student 56,696 Points

You need to select the children of the navigation unordered list. Try

//Select all listItems from the navigation
var listItems = navigation.children;

thanks for clarification