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

147
Visualizações
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 Respostas
Responde à pergunta

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 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