Skip to content

Instantly share code, notes, and snippets.

@smowden
Last active December 11, 2015 17:19
Show Gist options
  • Save smowden/4634105 to your computer and use it in GitHub Desktop.
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)
(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