public
Last active

Convert element to object then to json

  • Download Gist
elementToObject
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
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);
*/

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.