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!

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

Matija Skriljak
Matija Skriljak
3,338 Points

How to make this work??? With my code editor and 2 different solutions task is accomplished, but not here, again!!

I have two solutions, both do the work, but can't work with that test???

//Select the navigation
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++) {
<!DOCTYPE html>

<ul id="navigation">
    <a href="#home">Home</a>
    <a href="#about">About</a>
    <a href="#contact">Contact</a>

<p>A few of my favourite things:</p>
    Rain drops on roses
    Whiskers on kittens
    Brown paper packages wrapped up with string

<script src="app.js"></script>

2 Answers

Since its not a default click element you can just bind the click handler to the list item with

     var anchor = listItem.querySelector("a");
      anchor.bind('click', linkListener);

best practice to query the selector for the element to ensure your adding the listener to the right element

like the above answer