Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

JavaScript JavaScript and the DOM (Retiring) Traversing the DOM Getting All Children of a Node with children

Joseph Bertino
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



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,765 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!