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
MOD
Jonathan Grieve
Treehouse Moderator 91,253 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) => {
    e.preventDefault();
    //console.log(input.value);
    const text = input.value;
    const ul = document.getElementById('invitedList');
    const li = document.createElement('li');
    li.textContent = text;
    var clear = input.value = "";
    ul.appendChild(li)
});

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
231,236 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,253 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.