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

196
Views
How to remove all the child nodes of an element

I am just getting started off with javascript and I am trying to build a To-Do app. My problem is that I have a clear all button which when clicked removes all the tasks from the list. And to make it work, I have written this function(see below).

function clearAll(e){
e.preventDefault()

let allItems = todoList.childNodes
allItems.forEach(function(item){
   todoList.removeChild(item)
})
      
  }
Here the

todoList

is a ul element to which the divs(tasks) are appended dynamically. The result is that I am getting alternate items removed when I click the button. How do I fix this?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The following removeAllChildNodes() function removes all the child nodes of a node:

function removeAllChildNodes(parent) {
    while (parent.firstChild) {
        parent.removeChild(parent.firstChild);
    }
}
const container = document.querySelector('#container');
removeAllChildNodes(container)
about 4 years ago · Juan Pablo Isaza Report

0

You don't need all these complex things when you can just remove the innerHTML!

todoList.innerHTML = ""
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!