Skip to content

Instantly share code, notes, and snippets.

Created November 25, 2010 03:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/714851 to your computer and use it in GitHub Desktop.
Save anonymous/714851 to your computer and use it in GitHub Desktop.
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);
};
@martinille
Copy link

This plugin seems to be more complex and has a little more functionality: https://github.com/martinille/jquery.deepest.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment