Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

230
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!