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

205
Views
How to remove a div present inside a div in Javascript

I'm trying to remove all the divs present with class='characterCard', inside another div with class='childContainer', when a button is pressed. I've tried .remove() and removeChild() as well, but it doesn't work. Instead when using .removeChild(), I get the following error: Failed to execute 'removeChild' on 'Node': The node to be removed is not a child of this node.

The Character cards are placed inside the childContainers, so the parent element is the childDiv

let childDiv = document.querySelector('.childContainer');
let charDiv = document.querySelector('.characterCard');

searchBtn.addEventListener('click', ()=> {
  childDiv.removeChild(charDiv);
})

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

0

Call remove on the child:

let childDiv = document.querySelector('.childContainer');
let charDiv = document.querySelector('.characterCard');

searchBtn.addEventListener('click', ()=> {
  charDiv.remove();
})
about 4 years ago · Juan Pablo Isaza Report

0

Because there are (likely) more than one .characterCard divs, you need to loop though all matching elements inside of .childDiv and remove one by one.

childDiv.querySelectorAll('.characterCard').forEach(el => el.remove())

let childDiv = document.querySelector('.childContainer');

searchBtn.addEventListener('click', ()=> {
  childDiv.querySelectorAll('.characterCard').forEach(el => el.remove())
})

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!