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

133
Views
how to pass argument to button onclick inside template literal when looping
function show(x){
  console.log(x)
}

users.forEach(item => {
    let content = document.createElement("div")
    content.classList.add("content")
    content.innerHTML = `                      
                 <input type="button" id='add_deposit' value="add" onclick="show(${item})"   /
`;
  list.append(content)

it outputs identifier not found. how do i pass corrosponding item inside button onclick function

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Use addEventListener so that the click handler can see the item in its closure. Inline handlers can only reference global variables, which doesn't work well inside a loop (or, at all, often) - best to avoid them whenever possible.

You also shouldn't use the add_deposit ID - there should only be at most one element with a particular ID in a document.

users.forEach(item => {
    const content = document.createElement("div")
    content.classList.add("content")
    content.innerHTML = `                      
                 <input type="button" value="add" />
    `;
    content.children[0].addEventListener('click', () => {
        show(item);
    });
    list.append(content)
});
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!