Created
March 5, 2016 13:08
-
-
Save ryanlaws/8e06a09cea11b6f0f598 to your computer and use it in GitHub Desktop.
Get all classes and IDs of DOM elements given a parent element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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