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 JavaScript and the DOM (Retiring) Traversing the DOM Getting All Children of a Node with children

Why does he give arguments to the function at the end?

he gave an argument to the function in the last step somewhere at 5 mins, why did he do that? he didnt have any arguments to the function when he made it, why didint it say error?

3 Answers

Steven Parker
Steven Parker
229,644 Points

At about 2 mins in the video, you can see that the "attachListItemButtons" function takes one parameter named "li", which represents the element that the new buttons are added to.

Aakash Srivastav
seal-mask
.a{fill-rule:evenodd;}techdegree
Aakash Srivastav
Full Stack JavaScript Techdegree Student 11,638 Points

As you can see attachListItemsButtons accept an argument li , which will be the current li item where the buttons are added to. Now whenever you will call the attachListItemsButtons , you have to provide it an argument , so he does. lis[i] represents a single li element on which button is added. Hope it helps.

But why???

I get that he gives the "li" argument, but I don't get the reason...

I simplified the example to test if you had to give the li (as in list element) for the argument, but it works with anything there.

Is it kinda like the event object?

      <ul>
        <li>one</li>
        <li>two</li>
        <li>three</li>
        <li>four</li>
      </ul>
const parent = document.getElementsByTagName('ul')[0];
const child = parent.children;



const newButtons = whatever => {

  let button = document.createElement('button');

      button.className = 'button';
      button.textContent = 'Button';

  whatever.appendChild(button);

}

for (let i = 0; i < child.length; i += 1) {
  newButtons(child[i]);
}

You can check it in https://codepen.io/danvao/pen/ERPxOq

Steven Parker
Steven Parker
229,644 Points

I don't understand what you mean by "it works with anything there". Your code is passing each of the list items now, but if you change the argument in your call, you will not get any buttons:

  newButtons("anything");  // insead of: newButtons(child[i])