public
anonymous / jquery.deepest.js
Created

jQuery 'deepest' plugin

  • Download Gist
jquery.deepest.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
$.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);
};

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.