Get all ancestors up to X

71 Views Asked by At

Given: X -> B -> C -> D -> Child I want jQuery to return all ancestors up to the first node that matches some selector.

jQuery $(Child).parents(X) will return only X as opposed to X, B, C, D. I know I can use parent() to build this array manually, but is there a better way?

1

There are 1 best solutions below

0
On

Oops. Found it: $(Child).parentsUntil(X)

Source: http://api.jquery.com/parentsUntil/