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

Joseph Bertino
seal-mask
.a{fill-rule:evenodd;}techdegree
Joseph Bertino
Full Stack JavaScript Techdegree Student 14,652 Points

Order of statements issue

Assume: All other code is as written by the instructor.

In addItemButton.addEventListener(), if I call

attachListItemButtons(li);

before

li.textContent = addItemInput.value;

the "Add Items" button creates another list item but does not create the buttons for that item.

What gives? Why is it necessary to set the textContent of a list item before attaching buttons to it?

1 Answer

Cameron Childres
Cameron Childres
11,820 Points

Hi Joseph,

From Mozilla's documentation on textContent:

Setting textContent on a node removes all of the node's children and replaces them with a single text node with the given string value.

So while you've added the button elements in your example they all get removed when textContent is set.

The function attachListItemButtons() makes use of appendChild, which inserts the buttons as the last children of your <li> element without overwriting the contents. Hope this helps!