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

143
Views
For of loop only removes half of the items (Javascript)

I tried to build a filter for some posts. When a year is selected the other year should be removed from the page. But only the half of the unselected year posts gets deleted. What am I doing wrong?

var filter2020 = document.getElementById('filter20');
var filter2021 = document.getElementById('filter21');
var posts = document.getElementsByClassName('article');

if (filter2020) {
  for (item of posts) {
    if (item.dataset.year == "Y2020") {
      item.remove();
    } else {
      console.log("no")
    }
  }
} else if (filter2021) {
  for (item of posts) {
    if (item.dataset.year == "Y2021") {
      item.remove();
    } else {
      console.log("no")
    }
  }
} else {

}
<div class="row filtered-news">
  <div class="article col-lg-4" data-year="Y2021"> </div>
  <div class="article col-lg-4" data-year="Y2020"> </div>
</div>

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!