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

231
Visualizações
Get id from foreach loop javascript

After looping the delete button, I've been trying to get the id so that I can use it with the onclick function but no luck yet.

var table = "";
                    res.data.forEach((value, key) => {
                      table += "<tr>";
                      table += "<td>"+value.post_held+"</td>";
                      table += "<td>"+value.established_officer+"</td>";
                      table += "<td>"+value.date_of_commencement+"</td>";
                      table += "<td>"+value.date_of_termination+"</td>";
                      table += "<td><button id=career"+value.id+" class='btn btn-danger'>Delete</button></td>";
                      table += "</tr>";


                      var career = document.querySelector("#career"+value.id);
                      career.addEventListener("click",()=>{
                        alert("done")
                      })
                    });
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I think maybe you forgot to add table to DOM. You just create a string value which contain html code, but you didn't add this html code into the DOM, so you can not find this DOM element by id. You can use insertAdjacentHTML method to add this element which you want to DOM

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