Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

206
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda