Skip to content

Instantly share code, notes, and snippets.

@nathanhammond
Created August 13, 2008 15:18
Show Gist options
  • Save nathanhammond/5240 to your computer and use it in GitHub Desktop.
Save nathanhammond/5240 to your computer and use it in GitHub Desktop.
Functionality to add a filter for descendants and ancestors of a jQuery set.
/*
Ancestry - jquery.ancestry.js
As discussed in the jQuery Development Google Group.
Released under the MIT license.
Involved: Michael Geary, Diego Perini, John-David Dalton, John Resig, and Nathan Hammond
Compiled: Nathan Hammond
*/
jQuery.comparePosition = function ( element, context) {
jQuery.comparePosition =
document.documentElement.compareDocumentPosition ?
function ( element, context ) {
return !!( element.compareDocumentPosition(context) & 8 );
}
: document.documentElement.contains ?
function ( element, context ) {
return element != context && context.contains(element);
}
:
function ( element, context ) {
for ( ; element != context; element = element.parentNode )
if ( !element ) return false;
return true;
};
return jQuery.comparePosition( element, context );
}
jQuery.fn.ancestorOf = function ( context ) {
return this.filter(function() {
return jQuery.comparePosition( context, this );
});
};
jQuery.fn.descendantOf = function ( context ) {
return this.filter(function() {
return jQuery.comparePosition( this, context );
});
};
/*
Noted here if you wish to add parallel functionality to $()
if ( selector.nodeType ) {
// Handle $(DOMElement, context)
if ( context && !jQuery.comparePosition( selector, context ) ) {
return jQuery( [] );
}
// Handle $(DOMElement)
this[0] = selector;
this.length = 1;
return this;
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment