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

89
Vistas
Java Script Button.onclick doesnt'work if wrapped in div

In my project I need to create button dynamically. I wrap it to div and this div is appended to container called tableDoc. My problem is that in this case onclick function doesn't work.
If my button is appended straight to tableDoc with not being wrapped to div before it works fine.

Could you tell me why onclick function doesn't work if wrapped to div?

Example with button wrapped to div before being added to tableDoc:

 Elements.map(item=>{
           
            const div=document.createElement('div');
            ///////adding button////////////////
            var deleteButton = document.createElement("button");

            deleteButton.setAttribute('type','button');
            deleteButton.setAttribute('class',"btn-close");
            deleteButton.setAttribute('aria-label','Close');
            deleteButton.setAttribute('id',item.pk);
            deleteButton.onclick = function() { 
            alert("blabla");
          };
            //////////////////////////////////////////////////////
            div.innerHTML+=item.fields.car+'  '+item.fields.model
            div.appendChild(deleteButton); 
            tableDoc.appendChild(div);
            div.innerHTML+='<br>'


            })

secondary example with button added straight to tableDoc:

Elements.map(item=>{
           
            const div=document.createElement('div');
            ///////adding button////////////////
            var deleteButton = document.createElement("button");

            deleteButton.setAttribute('type','button');
            deleteButton.setAttribute('class',"btn-close");
            deleteButton.setAttribute('aria-label','Close');
            deleteButton.setAttribute('id',item.pk);
            deleteButton.onclick = function() { 
            alert("blabla");
          };
            //////////////////////////////////////////////////////
            div.innerHTML+=item.fields.car+'  '+item.fields.model
            tableDoc.appendChild(deleteButton);
            tableDoc.appendChild(div);
            div.innerHTML+='<br>'


            })
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You remove the event listener by using div.innerHTML+='<br>'.

In the below example, I am creating 2 buttons and append them to 2 divs. The only difference here is that afterwards, I use innerHTML on one of the divs which causes the button to lose its event listener.

That's because innerHTML is a text based function and doesn't carry event listeners with. It operates only on the string representation of the node.

const fooButton = document.createElement('button')
fooButton.onclick = () => console.log("foo")
fooButton.textContent = "foo"
document.getElementById("foo").append(fooButton)

const barButton = document.createElement('button')
barButton.onclick = () => console.log("bar")
barButton.textContent = "bar"
document.getElementById("bar").append(barButton)
document.getElementById("bar").innerHTML += '<br>'
<div id="foo"></div>
<div id="bar"></div>

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