I have a problem with a table that I made in html and javascript. The page is composed of 3 input fields that with the entered value is forwarded to the table below.
But I would like to be able to edit table items without having to delete and insert again.
Is there a way to send the values back to the input so that I can edit it?
PS:
I did the removal with a checkbox field. which takes the line and deletes, maybe to use this checkbox.
var itens = 0;
function adicionarItem() {
itens += 1;
var refNome = document.querySelector("#nome").value;
var refCpf = document.querySelector("#cpf").value;
var refRg = document.querySelector("#rg").value;
var usuarioTr = document.createElement("tr");
usuarioTr.className = itens %2 == 0 ? "linha1": "linha2";
var selec = document.createElement('td');
selec.className = 'usr';
var input = document.createElement('input');
input.type = 'checkbox';
input.className = 'check';
selec.appendChild(input);
var nomeTd = document.createElement("td");
var cpfTd = document.createElement("td");
var rgTd = document.createElement("td");
nomeTd.textContent = refNome;
cpfTd.textContent = refCpf;
rgTd.textContent = refRg;
usuarioTr.appendChild(selec);
usuarioTr.appendChild(nomeTd);
usuarioTr.appendChild(cpfTd);
usuarioTr.appendChild(rgTd);
var tabela = document.querySelector("#tabela");
tabela.appendChild(usuarioTr);
}
function removerItem() {
ckList = document.querySelectorAll("input[type=checkbox]");
ckList.forEach(function(el, index) {
if (ckList[index].checked) el.parentElement.parentElement.remove();
});
if (itens > 0)
itens -= 1;
}
<h1>Cadastro de usuário</h1>
<link rel="stylesheet" type="text/css" href="main.css" />
<section>
<form>
<div class="inputs">
<div class="inside">
<label for="nome">*Nome:</label>
<input class="inp" id="nome" name="nome" type="text" placeholder="Digite seu nome aqui" autofocus/>
</div>
<div class="inside">
<label for="cpf">*CPF:</label>
<input class="inp" id="cpf" name="cpf" type="number" placeholder="Digite seu CPF" />
</div>
<div class="inside">
<label for="rg">*RG:</label>
<input class="inp" id="rg" name="rg" type="number" placeholder="Digite seu RG" />
</div>
</div>
<div class="botoes">
<button clas="bot" id="adicionarBotao" onclick="adicionarItem()" type="button">Adicionar</button>
<button clas="bot" id="excluirBotao" onclick="removerItem()" type="button">Excluir</button>
</div>
</form>
</section>
<div class="tabelas">
<table>
<thead>
<tr>
<th>Selecionar</th>
<th>Nome</th>
<th>CPF</th>
<th>RG</th>
</tr>
</thead>
<tbody id="tabela">
</tbody>
</table>
</div>