Skip to content

Instantly share code, notes, and snippets.

Created November 25, 2010 03:28
Show Gist options
  • Save anonymous/714851 to your computer and use it in GitHub Desktop.
Save anonymous/714851 to your computer and use it in GitHub Desktop.

Revisions

  1. @invalid-email-address Anonymous created this gist Nov 25, 2010.
    28 changes: 28 additions & 0 deletions jquery.deepest.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,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);
    };