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

146
Visualizações
selecting all elements and adding new element after each one

I have some simple vanilla code here. My goal is to grab all the sections, loop through them and after each one insert a new element:

let sectionSign = document.createElement('p');
sectionSign.innerHTML = '§'
sectionSign.style.marginTop = '1rem'

let allSections = document.querySelectorAll('section')

allSections.forEach((section) => {
    section.style.border = '1px solid red'
    section.after(sectionSign)
})

the style.border was just to make sure it selected all the elements that I wanted and it did. However, the sectionSign is only being added after the last section, not after each one. How can I resolve this?

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

0

Try this instead, you have to create a new element for each section.

let allSections = document.querySelectorAll('section')

allSections.forEach((section) => {
    let sectionSign = document.createElement('p');
    sectionSign.innerHTML = '§';
    sectionSign.style.marginTop = '1rem';
    section.style.border = '1px solid red';
    section.after(sectionSign);
});
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