Skip to content

Instantly share code, notes, and snippets.

@ryanlaws
Created March 5, 2016 13:08
Show Gist options
  • Save ryanlaws/8e06a09cea11b6f0f598 to your computer and use it in GitHub Desktop.
Save ryanlaws/8e06a09cea11b6f0f598 to your computer and use it in GitHub Desktop.
Get all classes and IDs of DOM elements given a parent element
function getClassesAndIds(parent, memo) {
if (typeof memo === 'undefined')
memo = {classes: [], ids: []};
if (parent.className)
parent.className.split(' ').forEach(function (className) {
if (className && !~memo.classes.indexOf(className))
memo.classes.push(className)
});
if (parent.id)
memo.ids.push(parent.id);
[].forEach.call(parent.childNodes, function (node) {
memo = getClassesAndIds(node, memo)
});
return memo;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment