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 DOM Scripting By Example Adding and Removing Names Registering Names

Steve Moore
Steve Moore
6,562 Points

For the mental exercise...

Hello - Playing around with the code to see what works and what doesn't led me to a couple questions. I am looking for an explanation as to why the code below doesn't clear the text in the list item immediately after populating it as well as clearing the input.value since it is also assigned to the text variable. Not to suggest this is desirable behavior of course. Just curious. Thank you.

form.addEventListener('submit', (e) => {
  e.preventDefault();
let text = input.value; 
const ul = document.querySelector('#invitedList');
 const li = document.createElement('li');  
  li.textContent = text;
  ul.appendChild(li);
  text = "";
});

1 Answer

Shane Oliver
Shane Oliver
19,965 Points

Because text and li.textContent are not the same. Text and Li have different memory addresses. You can see that as they are both stored in different variables respectively