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

193
Visualizações
addEventListener not working for dynamic button

I have a dynamically made button, i've set the id and class to try to set an addEventListener but the addEventListener is not read in the DOM but the ID and class is.

here is my code

 let button: HTMLButtonElement = document.createElement('button')

  button.id = "comment-button";
  button.className = "comment-button"; 

(ID & class is working)

Here is what i've tried:

  button.addEventListener("click", setComment);

  document.getElementById("comment-button")?.addEventListener("click", setComment);

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

To retrieve an element from the document, you need to actually add the element you created to the document. document.body.appendChild(button)

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