CRUD with Javascript

Well, I need to do a CRUD and then a search on what is submitted in my form.

I have already created the page and I can already submit my form and show in a table.

Doubt: I am in doubt of how to remove and change my already submitted object.

var contatos = [ 
  document.querySelector("#campo-nome"),
  document.querySelector("#campo-endereco"),
  document.querySelector("#campo-bairro"),
  document.querySelector("#campo-telefoneFixo"),
  document.querySelector("#campo-celular"),
];
//console.log(contatos);
  
document.querySelector('#formulario').addEventListener("submit", function(event){

  event.preventDefault();

  var tr = document.createElement('tr');

  /*campo é o meu elemento, como se fosse o i.*/
  contatos.forEach(function(posicao) {
    td = document.createElement('td');
    td.textContent = posicao.value;
    tr.appendChild(td);
  });

  var tabela = document.querySelector("table tbody");

  tabela.appendChild(tr);

  for(var i=0;i <=contatos.length;i++){
    this[i].value ='';
  }

  contatos[0].focus();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<header>
  <div class="container">
    <h1>Agenda Contatos</h1>
  </div>
</header>
<main class="container">
  <section>
    <h2>Meus Contatos</h2>
    <table>
      <tr>
        <th>Nome</th>
        <th>Rua</th>
        <th>Bairro</th>
        <th>Telefone Fixo</th>
        <th>Telefone Celular</th>
      </tr>
      <tbody class="contato">
        
      </tbody>
    </table>
  </section>
  <section>
    <h2>Cadastro de Contatos</h2>
    <form id="formulario" >
      <fieldset>
        <label for="campo-nome">Nome:</label>
        <input id="campo-nome" type="text" placeholder="digite o nome do seu Contato">
      </fieldset>
      <fieldset class="campo-endereco">
        <label for="campo-endereco">Rua:</label>
        <input id="campo-endereco" type="text" placeholder="digite o endereço do seu Contato">
      </fieldset>
      <fieldset class="campo-bairro">
        <label for="campo-bairro">Bairro:</label>
        <input id="campo-bairro" type="text" placeholder="digite o seu bairro do seu Contato">
      </fieldset>
      <fieldset class="campo-endereco">
        <label for="campo-telefoneFixo">Telefone Fixo:</label>
        <input id="campo-telefoneFixo" type="text" placeholder="digite o telefone Fixo">
      </fieldset>
      <fieldset class="campo-TelefoneCelular">
        <label for="campo-celular">Telefone Celular:</label>
        <input id="campo-celular" type="text" placeholder="digite o telefone Celular">
      </fieldset>
      <button id="adicionar-contato" class="botao bto-principal">Adicionar</button>
    </form>
  </section>
</main>
Author: Laércio Lopes, 2017-08-20

1 answers

To remove and change you will do the same process by passing the ID.

Remove

To remove, very simple, you will click on the record of the table you clicked, and send the selected ID. This way you do a delete by passing the ID.

Update

The update is like you did in create. But you will send the ID. In case, you do an update passing the ID also that will send, and changes the fields.

 1
Author: Guilherme IA, 2017-08-21 18:06:38