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) Making Changes to the DOM Appending Nodes

Lisa Walters
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Lisa Walters
Front End Web Development Techdegree Graduate 15,255 Points

In line 28, where there is "ul.appendChild(li); , why are there no quotation marks around the li element? Thank you!

It seems that there are never quote marks around the parameter in appendChild. Why is that? Why are quotation marks included in the Selector methods? I would so much appreciate any clarification on this issue. Thanks.

1 Answer

Cameron Childres
Cameron Childres
11,820 Points

Hi Lisa,

On line 28 you're passing in li as a variable that you've defined on the previous 2 lines of code:

// make a list item element and store it in the variable li
let li = document.createElement('li');
// insert the input value in to element in the li variable
li.textContent = addItemInput.value;

We can name this variable anything we like as long as it's consistent. I could rewrite this block with something like newListItem instead and it works just the same:

addItemButton.addEventListener('click', () => {
  let ul = document.getElementsByTagName('ul')[0];
  let newListItem = document.createElement('li');
  newListItem.textContent = addItemInput.value;
  ul.appendChild(newListItem);
  addItemInput.value = '';
});

To dig in to it deeper, the querySelector() method accepts a string as its parameter -- hence the quotation marks. The appendChild() method accepts a node object as a parameter instead, which we created with document.createElement('li'), stored in a variable, and then passed in to it with the variable we defined.

To check this yourself take a look the documentation on these 3 methods and scroll down to the 'parameters' area:
querySelector()
createElement()
appendChild()

I hope this helps! If you have any questions let me know and I'll do my best to clear things up.