I have the following code:
var addItem = document.getElementById("add-item");
var formItem = document.getElementById("form-item");
var itens = [];
var idItem = 0;
var idDiv = 0;
addItem.addEventListener("click", function(){
var nome = document.getElementById("nome-item");
var codigo = document.getElementById("cod-item");
var descricao = document.getElementById("desc-item");
var qtd = document.getElementById("qtd-item");
var valor = document.getElementById("valor-item");
idItem++;
adicionaItem(idItem, nome, codigo, descricao, qtd, valor);
});
function adicionaItem(idItem, nome, codigo, descricao, qtd, valor){
var novoItem = {};
novoItem['id'] = idItem;
novoItem['nome'] = nome.value;
novoItem['codigo'] = codigo.value;
novoItem['descricao'] = descricao.value;
novoItem['quantidade'] = qtd.value;
novoItem['valor'] = valor.value;
itens.push(novoItem);
mostraItens(itens, novoItem);
console.log(idItem);
}
function mostraItens(itens, novoItem){
var tagLinha = document.createElement("DIV");
tagLinha.className = "row";
tagLinha.setAttribute("id", idDiv);
formItem.appendChild(tagLinha);
var botaoDel = document.createElement("BUTTON");
botaoDel.setAttribute("type", "button");
botaoDel.setAttribute("onclick", "removeItem(itens)");
botaoDel.textContent = "Excluir";
tagLinha.appendChild(botaoDel);
idDiv++;
}
function removeItem(itens){
console.log(itens.indexOf());
}
In the removeItem
function log I would like to find the index of the object with a specific id, but I do not know how to do it. I would like to know how to use indexOf () correctly.