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

120
Visualizações
how can I add onclick or eventListener while appending a button using javascript

well I am trying to append a button when user clicks an add button, also the button which is getting appended should have an onclick attribute or eventListener() to call deleteMyOrder() so how can I achieve this? I have done the following thing but it is still not working:

<body>
<div id="insertHere"> </div>
<input type="button" value="addButton" onclick="myFunc()" />

<script>
function myFunc()
{
var cell4 = document.getElementById('insertHere');
var element3 = document.createElement("input");
element3.type = "button";
element3.name = "add";
element3.id = "forRemove";
element3.value="Remove";
element3.className="btn btn-outline-danger btn-sm";
element3.onclick = "deleteMyOrder()";
cell4.appendChild(element3);
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can add event listener on the dynamically created button like this

element3.addEventListener('click', () => { console.log('button clicked') })

<body>
<div id="insertHere"> </div>
<input type="button" value="addButton" onclick="myFunc()" />

<script>
function myFunc()
{
  var cell4 = document.getElementById('insertHere');
  var element3 = document.createElement("input");
  element3.type = "button";
  element3.name = "add";
  element3.id = "forRemove";
  element3.value="Remove";
  element3.className="btn btn-outline-danger btn-sm";
  element3.addEventListener('click', () => { 
      console.log('button clicked');  // your click action here
    })
  cell4.appendChild(element3);
   
}
</script>

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