Skip to content

Instantly share code, notes, and snippets.

@barneycarroll
Created April 25, 2014 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save barneycarroll/11288260 to your computer and use it in GitHub Desktop.
Save barneycarroll/11288260 to your computer and use it in GitHub Desktop.
Log out a tree structure of a DOM element's ancestry
function getParentStructure( el ){
var $el = $( el );
var indent = '';
var output = '';
_.chain( $el.parents() ).reverse().map( function convertToString( parent ){
var attributes = parent.attributes;
output += indent + '<' + parent.tagName.toLowerCase();
if( attributes.length ){
_.each( attributes, function( attribute ){
output += ' ' + attribute.name + '="' + attribute.value + '"';
} );
}
output += '>\n';
indent += '\t';
} );
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment