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

206
Views
Select all elements except the first

I would like to know how to use only JavaScript to select all elements except the first one.

My current approach:

const ps = document.querySelectorAll('div > p')

const [first, ...rest] = ps

rest.forEach(p => {
  p.innerHTML = ""
})
<div>
  <p><span>+</span>1</p>
  <p><span>+</span>2</p>
  <p><span>+</span>3</p>
</div>

This works, but it returns an array instead of a NodeList. I would like it to return a NodeList.

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

0

CSS Selector: :not(:first-child)

const ps = [ ...document.querySelectorAll('div > p:not(:first-child)') ]
console.log(ps.map(e => e.innerHTML));
<div>
  <p><span>+</span>1</p>
  <p><span>+</span>2</p>
  <p><span>+</span>3</p>
</div>


Splice: ps.splice(0, 1);

const ps = [ ...document.querySelectorAll('div > p') ]
ps.splice(0, 1);
console.log(ps.map(e => e.innerHTML));
<div>
  <p><span>+</span>1</p>
  <p><span>+</span>2</p>
  <p><span>+</span>3</p>
</div>

about 4 years ago · Juan Pablo Isaza Report

0

const ps = document.querySelectorAll("div > p:not(:first-child)");
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!