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

148
Vistas
AddEVentListener not working after updated html button
<!-- Any html blocks -->
{% for n in loop %}
<div class="update">
    <div>
        <button type="button" class="update--btn">-</button>
    </div>
</div>
{% endfor %}


let updateBtn = document.getElementsByClassName('update--btn');

[...updateBtn].forEach(e => {
    e.addEventListener('click', clickBtn);
})

function clickBtn() {
    const xhr = new XMLHttpRequest();
    xhr.open('POST', url);
    xhr.onload = () => {
        let cartUpdate = this.closest('.update');
        cartUpdate.innerHTML ='';

        let cartUpdateInner = document.createElement('div');
        cartUpdateInner.innerHTML = '<button type="button" class="update--btn">+</button>'
        cartUpdate.appendChild(cartUpdateInner);
    }
    xhr.send();
}

When the page is loaded for the first time, all working. But second click on button, after change HTML in not working.

PS. I need change html, not only text

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

0

you can put your update button code in a function like

function addevent(){
[...updateBtn].forEach(e => {
    e.addEventListener('click', clickBtn);
})
}

and then call it on page load for first time

window.onload = addevent;

and then you need to reassign this event after adding new buttons so you can call this in the end of your clickBtn function

addevent()
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