Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

112
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda