I am creating a list of items with JQuery that is rendered on the screen according to what is typed in the input.
To display the on-screen element (item name, change button, and remove button) javascript adds the value typed into an array of objects along with an ID that I called idSymbolic to be able to identify each item in time of deleting or changing an item, then a array.map()
is performed to render the array items on the screen.
But the problem is that every time I type new text to create and render the new item, array.map renders everything in the array including the items already on the screen, making my list redundant. / p>
My doubts are as follows:
1 - How do I make the array.map render only the items that are no longer on the screen?
2 - When deleting an item from the screen by clicking the remove button, how do I delete only that clicked item (from screen and array).
HTML
<div class="">
<div id="lista-itens">
<div id="titulo">Lista de Itens</div>
</div>
</div>
<div class="">
<input type="text" id="txtItem">
<button type="button" onClick="save()">SALVAR</button>
</div>
Javascript / JQUERY
var itens = [];
var idSimbolico = 0;
function save(){
var id = idSimbolico += 1;
var txtItem = $('#txtItem').val();
itens.push({
id: idSimbolico,
nome: txtItem
});
itens.map(item => {
$("#titulo").append('
<div id="item">
<div id="nomeDoItem">${item.nome}</div>
<input type="hidden" value="${item.id}">
<div>
<button>Alterar</button>
<button>Deletar</button>
</div>
</div>
');
});
}