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 trialJoseph Bertino
Full Stack JavaScript Techdegree Student 14,652 PointsOrder 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
11,820 PointsHi 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!
Joseph Bertino
Full Stack JavaScript Techdegree Student 14,652 PointsJoseph Bertino
Full Stack JavaScript Techdegree Student 14,652 PointsI now wish I checked out the documentation of textContent! Thanks!