Created
April 25, 2014 12:39
-
-
Save barneycarroll/11288260 to your computer and use it in GitHub Desktop.
Log out a tree structure of a DOM element's ancestry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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