Created
October 19, 2011 15:44
-
-
Save alain75007/1298684 to your computer and use it in GitHub Desktop.
Convert element to object then to json
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 elementToObject(element, o) { | |
var el = $(element); | |
var o = { | |
tagName: el.tagName | |
}; | |
var i = 0; | |
for (i ; i < el.attributes.length; i++) { | |
o[el.attributes[i].name] = el.attributes[i].value; | |
} | |
var children = el.childElements(); | |
if (children.length) { | |
o.children = []; | |
i = 0; | |
for (i ; i < children.length; i++) { | |
child = $(children[i]); | |
o.children[i] = elementToObject(child, o.children) ; | |
} | |
} | |
return o; | |
} | |
/* | |
exemple: | |
a = elementToObject(document.body); | |
Object.toJSON(a); | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment