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

110
Views
Problem with for loop in a to-do website - JavaScript

I have a problem with creating a for loop that will remove all my to-do lists. I've been coding for a week so this might sound like a really stupid question. The code is all in a function and is run by a buttons onclick attribute.

    let toDoList = document.querySelector(".to-do-list");
    let newListClass = document.getElementsByClassName('new-list');
    let newList = document.querySelector('.new-list');

    console.log(newList);
    console.log(newListClass.length);
    
    for (i = 0; i < newListClass.length; i++) {
        newList.remove();
    }
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Two main options that I see:

// Option A) getting elements by class name
const newListClass = document.getElementsByClassName('new-list');
console.log(newListClass);

for (i = 0; i < newListClass.length; i++) {
    newListClass[i].remove();
}

// Option B) getting elements with a selector, notice the "All"
const newListClass = document.querySelectorAll('.new-list');
console.log(newListClass);

for (i = 0; i < newListClass.length; i++) {
    newListClass[i].remove();
}

You could also use forEach, but a classic for loop is always good

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!