Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

146
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!