Skip to content

Instantly share code, notes, and snippets.

@morten-olsen
Last active August 29, 2015 14:21
Show Gist options
  • Save morten-olsen/ab2a0cd669d566abb654 to your computer and use it in GitHub Desktop.
Save morten-olsen/ab2a0cd669d566abb654 to your computer and use it in GitHub Desktop.
A simple gist for easily extracting elements from a DOM element into a pretty hierarchical structure
Node.prototype.find = function (elms) {
if (typeof elms === 'string') {
return this.querySelector(elms);
}
var result = {};
for (var key in elms) {
result[key] = this.find(elms[key]);
}
return result;
};
var details = document.find({
header {
logo: '#header img',
title: '#header h1'
},
article : 'article'
});
details.header.title.innerHTML = 'test';
document.find('#header h1').innerHTML = 'test';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment