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?
$('#element').parent().parent().parent()
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".
2.1m questions
2.1m answers
60 comments
57.0k users