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

147
Visualizações
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 Respostas
Responde à pergunta

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 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