Making a appendChild
you are adding a new list in div without removing or replacing the one that is there.
You can do this in two ways:
Method 1: Emptying the div with document.querySelector('#app').innerHTML = "";
before doing the appendChild
in function adicionar()
:
function adicionar() {
var inputElement = document.querySelector("#nome").value;
//nomes = inputElement;
nomes.push(inputElement);
document.querySelector('#app').innerHTML = ""; // esvazia a div
document.querySelector('#app').appendChild(list(nomes));
}
Method 2: Directly with innerHTML
:
function adicionar() {
var inputElement = document.querySelector("#nome").value;
//nomes = inputElement;
nomes.push(inputElement);
document.querySelector('#app').innerHTML = list(nomes).outerHTML;
}
Only that the function list(nomes)
is returning an object with the elements and not HTML. In this case you should use outerHTML
to convert this object to HTML to work with innerHTML
.
Examples
Method 1: Emptying the div:
var nomes = ['Diego', 'Gabriel', 'Lucas'];
//var btnElement = document.querySelector('button.btn');
function adicionar() {
var inputElement = document.querySelector("#nome").value;
//nomes = inputElement;
nomes.push(inputElement);
document.querySelector('#app').innerHTML = ""; // esvazia a div
document.querySelector('#app').appendChild(list(nomes));
}
function list(array) {
//Cria a lista do elemento
var listElement = document.createElement("ul");
//listElement.setAttribute('id', 'myList');
for (var i = 0; i < array.length; i++) {
//Cria a lista de item
var itemElement = document.createElement('li');
//Defini seu conteudo
itemElement.appendChild(document.createTextNode(array[i]));
//Adiciona um item a lista
listElement.appendChild(itemElement);
}
return listElement;
}
// Add the contents of options[0] to #foo:
document.querySelector('#app').appendChild(list(nomes));
<div class="container">
<div class="form-inline">
<div class="form-group mx-sm-3 mb-2">
<label class="sr-only">Nome</label>
<input type="text" class="form-control" id="nome" placeholder="Nome">
</div>
<button type="submit" class="btn btn-primary mb-2" onclick="adicionar()">Adicionar</button>
</div>
<div id="app"></div>
</div>
Method 2: With innerHTML:
var nomes = ['Diego', 'Gabriel', 'Lucas'];
//var btnElement = document.querySelector('button.btn');
function adicionar() {
var inputElement = document.querySelector("#nome").value;
//nomes = inputElement;
nomes.push(inputElement);
document.querySelector('#app').innerHTML = list(nomes).outerHTML;
}
function list(array) {
//Cria a lista do elemento
var listElement = document.createElement("ul");
//listElement.setAttribute('id', 'myList');
for (var i = 0; i < array.length; i++) {
//Cria a lista de item
var itemElement = document.createElement('li');
//Defini seu conteudo
itemElement.appendChild(document.createTextNode(array[i]));
//Adiciona um item a lista
listElement.appendChild(itemElement);
}
return listElement;
}
// Add the contents of options[0] to #foo:
document.querySelector('#app').appendChild(list(nomes));
<div class="container">
<div class="form-inline">
<div class="form-group mx-sm-3 mb-2">
<label class="sr-only">Nome</label>
<input type="text" class="form-control" id="nome" placeholder="Nome">
</div>
<button type="submit" class="btn btn-primary mb-2" onclick="adicionar()">Adicionar</button>
</div>
<div id="app"></div>
</div>
I got Sam thanks for the help and the explanation.
– Henrique
Sam and if I wanted to clear the field after adding a new name, how would I do it? If I put inside a form it updates the page after clicking the add button
– Henrique
To clear the field put at the end of the function:
document.querySelector("#nome").value = "";
– Sam
You can put it inside a form if you want to submit the form for something, like write it to the database or send it to PHP.
– Sam