Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  147] [ 1]  / answers: 1 / hits: 125963  / 13 Years ago, wed, august 17, 2011, 12:00:00

When I want to get, for example, the 3rd level parent of the element I must write $('#element').parent().parent().parent() Is there a more optimal method for this?


More From » jquery

 Answers
29

Since parents() returns the ancestor elements ordered from the closest to the outer ones, you can chain it into eq():



$('#element').parents().eq(0);  // Father.
$('#element').parents().eq(2); // Great-grandfather.

[#90563] Tuesday, August 16, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leiaf

Total Points: 10
Total Questions: 101
Total Answers: 84

Location: Guam
Member since Tue, Nov 29, 2022
2 Years ago
leiaf questions
Sat, Mar 27, 21, 00:00, 3 Years ago
Wed, Apr 3, 19, 00:00, 5 Years ago
Wed, Jan 16, 19, 00:00, 6 Years ago
;