Search code examples
jqueryfilterancestordescendant

Jquery: get ancestors (or descendants) and self


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() ?


Solution

  • You can do this:

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

    For parents:

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