Jquery: get ancestors (or descendants) and self

gsakkis picture gsakkis · Mar 24, 2010 · Viewed 9.7k times · Source

One can use matchedset.find(selector) / matchedset.parents(selector) to get the descendants/ancestors of the current matched set filtered by a selector, but that doesn't include the matched set itself (if it happens to match the selector too). Is there a better (more concise and/or faster) way to get it than

matchedset.find(selector).add(matchedset.filter(selector))

and the respective for parents() ?

Answer

Nick Craver picture Nick Craver · Mar 24, 2010

You can do this:

matchedset.find('*').andSelf().filter(selector);

For parents:

matchedset.parents('*').andSelf().filter(selector);