jQuery: get a specific ancestor of an element

If you want to get a grandparent or great grandparent element you can
Here’s a nicer way to get an ancestor element without chaining calls to parent().parent() etc.

// use parent() for the parent element
// use parents() for a specific level of ancestor
$(this).parents(':eq(0)'); // parent
$(this).parents(':eq(1)'); // grandparent
$(this).parents(':eq(2)'); // great grandparent
// or for an ancestor with a specific selector

Comments are closed.