javascript – Delete table row by checkBox

Question:

I'm trying to use a checkbox to delete several rows from a table created by javaScript with input information. But I don't know how to make him recognize the td so I can execute the delete function(Button);

function adicionarItem() {
  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 = "user";

  var selec = document.createElement('td');
  selec.className = 'usr';
  var input = document.createElement('input');
  input.type = 'checkbox';
  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);
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>Teste01</title>
</head>

<body>
  <link type="text/css" href="main.css" />
  <script src="./main.js"></script>

  <section>
    <form>
      <div>
        <label for="nome">Nome:</label>
        <input id="nome" name="nome" type="text" placeholder="Digite seu nome aqui" autofocus/>
      </div>
      <div>
        <label for="cpf">CPF:</label>
        <input id="cpf" name="cpf" type="number" placeholder="Digite seu CPF" />
      </div>
      <div>
        <label for="rg">RG:</label>
        <input id="rg" name="rg" type="number" placeholder="Digite seu RG" />
      </div>

      <button id="adicionarBotao" onclick="adicionarItem()" type="button">Adicionar</button>
      <button id="editarBotao" onclick="" type="button">Editar</button>
      <button id="excluirBotao" onclick="" type="button">Excluir</button>
    </form>
  </section>

  <table>
    <thead>
      <tr>
        <th>Selecionar</th>
        <th>Nome</th>
        <th>CPF</th>
        <th>RG</th>
      </tr>
    </thead>
    <tbody id="tabela">

    </tbody>
  </table>

</body>

</html>

Answer:

You can delete with the following function:

function removerItem() {
  ckList = document.querySelectorAll("input[type=checkbox]:checked");
  ckList.forEach(function(el) {
    el.parentElement.parentElement.remove();
  });
}

where ckList is a list with all input elements of type checkbox that are checked ( checked ), and forEach care of removing all tr's where these elements are.

function adicionarItem() {
  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 = "user";

  var selec = document.createElement('td');
  selec.className = 'usr';
  var input = document.createElement('input');
  input.type = 'checkbox';
  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]:checked");
  ckList.forEach(function(el) {
    el.parentElement.parentElement.remove();
  });
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>Teste01</title>
</head>

<body>
  <link type="text/css" href="main.css" />
  <script src="./main.js"></script>

  <section>
    <form>
      <div>
        <label for="nome">Nome:</label>
        <input id="nome" name="nome" type="text" placeholder="Digite seu nome aqui" autofocus/>
      </div>
      <div>
        <label for="cpf">CPF:</label>
        <input id="cpf" name="cpf" type="number" placeholder="Digite seu CPF" />
      </div>
      <div>
        <label for="rg">RG:</label>
        <input id="rg" name="rg" type="number" placeholder="Digite seu RG" />
      </div>

      <button id="adicionarBotao" onclick="adicionarItem()" type="button">Adicionar</button>
      <button id="editarBotao" onclick="" type="button">Editar</button>
      <button id="excluirBotao" onclick="removerItem()" type="button">Excluir</button>
    </form>
  </section>

  <table>
    <thead>
      <tr>
        <th>Selecionar</th>
        <th>Nome</th>
        <th>CPF</th>
        <th>RG</th>
      </tr>
    </thead>
    <tbody id="tabela">

    </tbody>
  </table>

</body>

</html>
Scroll to Top
AllEscort