Skip to content

Instantly share code, notes, and snippets.

@fracmak
Forked from alain75007/elementToObject
Last active February 7, 2023 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fracmak/7452752 to your computer and use it in GitHub Desktop.
Save fracmak/7452752 to your computer and use it in GitHub Desktop.
/**
* 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;
}
@Foopss
Copy link

Foopss commented Feb 7, 2023

Uncaught ReferenceError: _ is not defined

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment