Last active
December 11, 2015 17:19
-
-
Save smowden/4634105 to your computer and use it in GitHub Desktop.
return all unique classes and ids (or other attributes) of children contained within a html element
(requires jquery)
This file contains hidden or 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(){ | |
var containedElems = $('#container *'); | |
var findAttribs = { | |
'class': [], | |
'id': [] | |
}; | |
containedElems.each(function(i, elem){ | |
for(var curAttrib in findAttribs){ | |
var elemAttrib = $(elem).attr(curAttrib) | |
if(typeof elemAttrib === 'string'){ | |
findAttribs[curAttrib] = findAttribs[curAttrib].concat(elemAttrib.split(" ")); | |
} | |
if(i === containedElems.length-1){ | |
findAttribs[curAttrib] = findAttribs[curAttrib].sort().filter(function(el,i,a){if(i==a.indexOf(el))return 1;return 0}) // http://stackoverflow.com/a/7076202/404799 | |
} | |
} | |
}) | |
return findAttribs; | |
}).call(this); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment