Skip to content

anonymous /jquery.deepest.js

jQuery 'deepest' plugin
$.fn.deepest = function() {
/// <summary>
/// Return the deepest child of this element.
/// For example, when run on this structure:
///
/// <div>
/// <span> <b>pick me!</b> </span>
/// <span> </span>
/// </div>
///
/// It would return the element:
///
/// <b>pick me!</b>
/// </summary>
var levels = 0;
var deepest;
$(this).find('*').each(function() {
if( !this.firstChild || this.firstChild.nodeType !== 1 ) {
var levelsFromThis = $(this).parentsUntil(this).length;
if(levelsFromThis > levels) {
levels = levelsFromThis;
deepest = this;
}
}
});
return $(deepest);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.