public
Last active — forked from alain75007/elementToObject

  • 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
/**
* Given an html element or jquery object, will return a serializable object representation of that node
* @param {HTMLElement|jQuery} element
* @param {Boolean} [recurse] - specifying whether we want to build objects for the children
* @returns {Object}
*/
function elementToObject(element, recurse) {
var el = $(element),
o = {
tagName: el[0].tagName
};
 
_.each(el[0].attributes, function(attribute){
o[attribute.name] = attribute.value;
});
 
if (recurse) {
o.children = _.map(el.children(), function(child){
return this.elementToObject(child, true);
}, this);
}
return o;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.