I need list of classes used in an html file. Is there any tool where i can get list of classes in the HTML file?
-
1Do you need just list of class names, or values also?Milan– Milan2011-09-24 10:44:57 +00:00Commented Sep 24, 2011 at 10:44
-
I need class name which use in the HTML like what ever class i apply to control or any other container.KuldipMCA– KuldipMCA2011-09-24 12:35:20 +00:00Commented Sep 24, 2011 at 12:35
6 Answers
This should work and it doesn't need jquery:
const used = new Set();
const elements = document.getElementsByTagName('*');
for (let { className = '' } of elements) {
for (let name of className.split(' ')) {
if (name) {
used.add(name);
}
}
}
console.log(used.values());
2 Comments
seen.add(name). It was supposed to be used.add(name). Elegant solution though, really good stuff.If you've got jQuery on the page, run this code:
var classArray = [];
$('*').each(function(){if(this.className!=""){classArray.push(this.className)}})
The variable classArray will contain all the classes specified on that HTML page.
2 Comments
Take a look at Dust Me Selectors.
It's not exactly what you are looking for, it shows a list of UNused selectors. However, I imagine you could use the inverse of the list it provides.
Here is the link: http://www.sitepoint.com/dustmeselectors/
1 Comment
I know this is an old question, but got here through google so I suspect more people can get here too.
The shortest way, using querySelectorAll and classList (which means, browser support could be an issue: IE10 for classList and IE8 for querySelectorAll ), and with duplicates, would be:
var classes = 0,
elements = document.querySelectorAll('*');
for (var i = 0; i < elements.length; i++) {
classes = classes + elements[i].classList.length;
}
I made a jsFiddle with a fallback for classList (which has the "lowest" browser support) that also counts all elements, all classes and all elements with classes if you're not using classList.
I didn't add a unique detection though, might get to it some day.
1 Comment
Quickly list classes from console (ignoring 'ng-*' angular classes)
(global => {
// get all elements
const elements = document.getElementsByTagName('*');
// add only unique classes
const unique = (list, x) => {
x != '' && list.indexOf(x) === -1 && x.indexOf('ng-') !== 0 && list.push(x);
return list;
};
// trim
const trim = (x) => x.trim();
// add to variable
global.allClasses = [].reduce.call(elements, (acc, e) => e.className.split(' ').map(trim).reduce(unique, acc), []).sort();
console.log(window.allClasses);
})(window);