Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.