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

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Dom Scripting and Memory Usage

Just a quick question on memory using.

When Guil started talking about cleaning the text from the input field when adding names, I decided to pause and jump ahead and this was my solution for clearing the value from the input field.

form.addEventListener('submit', (e) => {
    const text = input.value;
    const ul = document.getElementById('invitedList');
    const li = document.createElement('li');
    li.textContent = text;
    var clear = input.value = "";

I created a variable, clear for clearing the value of input to an empty string. Then Guil showed that I didn't need to add a variable. Is each way equally valid? I don't think it matters too much in this instance but maybe for a larger project, bandwidth might become an issue.

1 Answer

Steven Parker
Steven Parker
229,921 Points

The variable clear in the example serves no purpose.

The portion of the code that empties the form field is: input.value = "".

Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,252 Points

Hi Steven,

Thanks this clears it up anyway. So it doesn't need a variable as it's a method that performs an action on it's own and already has a reference to the element it's forming the clear string on.