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

Mina Soliman
Mina Soliman
3,349 Points

in var anchor = listItem.querySelector("a") How comes we are referring to (listItem) while it's not defined previously

all variables are referred to something already defined in the code, like var navigation > document > id .. And var listItems is referred to navigation > children..

while var anchor is referred to listItem which wasn't defined in the document before?? how is that?? how can the code find the <a> that's related to a listItem without knowing what the listItem is ??

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

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

//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>

2 Answers

Craig Campbell
Craig Campbell
14,428 Points

You are passing in "listItem" into the anonymous function that is being stored in the bindEventsToLinks variable. "listItem" itself is not defined, but it stand for WHATEVER you pass into that function.

When you actually are calling the function, your code is bindEventsToLinks(listItems[i]); So for each thing in listItems , you are using that to "replace" the word "listItem" in the code. It's not exactly how it works, but the idea is you are subbing in "listItems[0], listitems[1], listItems[2], etc." for the "listItem" every time the function is called in the for loop.

So think of "listItem" as a proxy for a real thing that will be passed in later. I hope that clears things up!

Mina Soliman
Mina Soliman
3,349 Points

Thanks a lot.. I got it now