Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

185
Visualizações
Looping though childNodes and deleting all leaves one entry not deleted

I have a function that iterates all options returned from an API fetch, creates a p tag for each, and appends them all to a parent div called options

    textOptions.forEach((text) => {
        let newText = document.createElement('p')
        newText.innerHTML = text
        newText.addEventListener('click', () => {
            replaceSelectedText(newText.innerHTML, range)
            handleCloseMenu()
        })
        options.appendChild(newText)
    })

when the handleCloseMenu() function is executed, it is to check if the div options has any children and if so, remove them all, so that the next API call cacn return a fresh set of children.

const handleCloseMenu = () => {
    menu.style.display = 'none'
    if (options.hasChildNodes()) {
        options.childNodes.forEach((child) => {
            child.remove()
        })
    }
    console.log('OPTIONS CHILDREN', options.childNodes)
}

after the loop is done in my console.log I can see it has not deleted one of the children, it always leaves the second child. I also see it sitting there in my options during the next API call. Am I doing something incorrectly? I know I am of course but some info on what the problem is would be greatly appreciated

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

childNodes is a live collection:

options.childNodes.forEach((child) => {
  console.log('x');
  child.remove()
})
<div id="options"><div>1</div><div>2</div><div>3</div><div>4</div><div>5</div><div>6</div></div>

If you remove an element contained in the collection, the collection will change instantly - while you're iterating over it, so indicies will be lost if you iterate 0-1-2-3 etc like normal.

Turn it into an array instead, to extract all values into a static collection first.

[...options.childNodes].forEach(
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda