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

145
Views
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 answers
Answer question

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 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!