I made a simple list items page with JavaScript. And everything works just as expected. What I want is that, every time I add a new list-item, the input field should be cleared. As in the value should be deleted.
function addItem() {
if (document.querySelector('.text-input').value) {
const listItem = document.createElement('li');
listItem.className = 'list-item'
var input = document.querySelector('.text-input').value;
listItem.textContent = input;
document.querySelector('.list').appendChild(listItem);
document.querySelector('.text-input').value = '';
}
}
document.getElementById('btn').addEventListener('click', addItem);
And I actually achieved what I wanted with that last line of code in the addItem callback function. However instead of writing document.querySelector('.text-input').value = '';, if I write input = '';, it doesn't work. And this is not making any sense to me cause I declared that variable within that function and also used it for listItem.textContent = input; as you can see.
var input = document.querySelector('.text-input').valueit's a variable declaration initialized with a value. Doing thisinput = ''is only re-assigning another value. Probably you're confused about referencing the attributevalue.