Get list of all classes in an HTML page via javascript

html
javascript
styling-web
Category Software and digital electronics / Coding
2024-08-18 05:34

I have an HTML page with many classes. My concern is that I have not handled all element styles. I am looking for a way to get the list of all styles used in my HTML page.

Is there any javascript way to find the list al all HTML classes used for all elements?

Answered by robin
2024-08-18 10:23

Yes, it is possible via Javascript:

const classList = new Set(); // output
const allElements = document.querySelectorAll('*');

allElements.forEach(element => {
    element.classList.forEach(className => {
        classList.add(className); // Add each class name to the Set
    });
});

console.log([...classList]);
×

Login

No account?
Terms of use
Forgot password?