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

111
Views
Why are not all elments deleted?

I have a strange behavior in my very simple code. I want to remove() all buttons with the class btn from the DOM after a button has been clicked.

In my function removeAllBtns() I have two console outputs. The correct number of buttons is displayed. But the second element is always not deleted.

Why is that? I am probably missing something very simple here. Thanks in advance.

let btns = document.getElementsByClassName('btn');

for (var i = 0; i < btns.length; i++) {
  let button = btns[i];
  button.addEventListener('click', function (e) {  
    removeAllBtns();  
  });
}

function removeAllBtns() {  
  console.log('btns length',btns.length)
  for (let j = 0; j < btns.length; j++) {
    console.log(j, btns[j])
    btns[j].remove();
    
  }
}
.btn{
  color: red;
}
<button class="btn">1</button>   
<button class="btn">2</button>   
<button class="btn">3</button>

about 4 years ago · Juan Pablo Isaza
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!