Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

232
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda