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

145
Views
Appending button onclick function runs when appended

When I append a button with an onclick function, that same functions runs immediately. How do I keep the onclick function and make it not run when appended? Heres the code where it gets appended:

let button=document.createElement("button");
button.innerHTML=users[i].replace("user__","").replace("=","").substring(0,users[i].replace("user__","").replace("=","").length/2);
button.onclick=removeUser(button.innerHTML);
accounts.appendChild(button);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

As per your codebase, the button.onclick needs to be asigned as a function.

But in the code it is button.onclick=removeUser(button.innerHTML), due to this line removeUser would be directly executed when creating the button.

You might have to update your code as follow:

button.onclick = () => removeUser(button.innerHTML)

This way, the removeUser is wrapped inside a function and that function is only called when user clicks on the button. Hope this helps. :)

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!