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

226
Views
JS/jQuery: Get depth of element?

What's the easiest way to get the depth of an element in pure JavaScript or jQuery? By "depth" I mean how many elements deep is it nested, or how many ancestors does it have.

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

How about:

$('#my-element').parents().length
over 4 years ago · Santiago Trujillo Report

0

An additional note. If you want to get the depth relative to a certain context you can do:

var depth = $("#my-element","#ContextContainerID").parents("ul").length;

Above, I'm searching for how many UL's are within the container #ContextContainerID

over 4 years ago · Santiago Trujillo Report

0

Supposing you don't want to include body and html tag in the parents to count use:

$("#element").parents("*").not("body,html").size()

Online demo here: http://jsfiddle.net/zaJff/

over 4 years ago · Santiago Trujillo 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!