Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

165
Visualizações
Update a obj list in js with a form and show it in HTML page

I have a HTML page with a form and a table. In a js file I have a list of objects (JSON) and with this list I populate the table when the page loads.

The form should allow me to add an element to the list and then display it as a new row in the table.

This is my js file:

var data = [
    {
        "first_name": "John",
        "last_name": "Smith",
        "email": "john.smith@gmail.com",
        "job": "CEO",
        "agree": true
    },
    {
        "first_name": "Marie",
        "last_name": "Curie",
        "email": "marie.curie@gmail.com",
        "job": "Researcher",
        "agree": true
    }];
populateTable();
function populateTable() {
    var out = "<table><tr>"
        + "<th>First Name</th><th>Last Name</th>"
        + "<th>Email</th>"
        + "<th>Job</th><th>Agree</th></tr>";
    var i;
    for (i = 0; i < data.length; i++) {
        out += '<tr><td>' + data[i].first_name + '</td><td>' + data[i].last_name + '</td><td>'
            + data[i].email + '</td><td>' + data[i].job + '</td><td>' + data[i].agree + '</td></tr>';
    }
    document.getElementById("showData").innerHTML = out;
};

function collectData(){
    var TestName = document.getElementById("fname").value;
    var TestSurname = document.getElementById("lname").value;
    var TestEmail = document.getElementById("email").value;
    var TestJob = document.getElementById("job").value;
    var TestAgree = document.getElementById("agree_terms").value;
    
    //insert this data on JSON file
    var jsonObject = {
        "first_name": TestName, 
        "last_name": TestSurname,
        "email": TestEmail,
        "job": TestJob,
        "agree": TestAgree
    };
    data.push(jsonObject);
}

I tried to push the new element on the list but naturally the code reload the function populateTable() without considering this new element.

Ho can I resolve it?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Add populateTable to the end of the function

var data = [
    {
        "first_name": "John",
        "last_name": "Smith",
        "email": "john.smith@gmail.com",
        "job": "CEO",
        "agree": true
    },
    {
        "first_name": "Marie",
        "last_name": "Curie",
        "email": "marie.curie@gmail.com",
        "job": "Researcher",
        "agree": true
    }];
populateTable();
function populateTable() {
    var out = "<table><tr>"
        + "<th>First Name</th><th>Last Name</th>"
        + "<th>Email</th>"
        + "<th>Job</th><th>Agree</th></tr>";
    var i;
    for (i = 0; i < data.length; i++) {
        out += '<tr><td>' + data[i].first_name + '</td><td>' + data[i].last_name + '</td><td>'
            + data[i].email + '</td><td>' + data[i].job + '</td><td>' + data[i].agree + '</td></tr>';
    }
    document.getElementById("showData").innerHTML = out;
};

function collectData(){
    var TestName = "Demo";
    var TestSurname = "Demo";
    var TestEmail = "Demo";
    var TestJob = "Demo";
    var TestAgree = "Demo";
    
    //insert this data on JSON file
    var jsonObject = {
        "first_name": TestName, 
        "last_name": TestSurname,
        "email": TestEmail,
        "job": TestJob,
        "agree": TestAgree
    };
    data.push(jsonObject);
    populateTable();
    console.log("Click");
}
<div id="showData">

</div>
<button onclick="collectData()">
Add Demo Data
</button>

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda