I was wondering if there is an easy way to list all available css classes loaded withing a single html page in javascript or through the developer console in chrome or firefox.
Thank you
There's no easy way to find all available css classes, but we can find all those being used:
[...[...$$("[class]")].reduce((s, e) =>
(e.classList.forEach(c => s.add(c)), s), new Set())].sort()
Though wrapped just for readability :-)
There're two shorthands used in the above one liner:
$$
is a shorthand for document.querySelectorAll
[...iterable]
is for Array.from(iterable)
, see the spread syntax (...)And here's the expanded code:
Array.from( // 4
Array.from( // 2
document.querySelectorAll("[class]") // 1
).reduce( // 3
(s, e) => (e.classList.forEach(c => s.add(c)), s),
new Set()
)
).sort() // 5
class
attribute, see attribute selectorsArray.from
to turn the NodeList from step 1 into an ArrayArray.reduce
to collect the CSS classes into a Set, to remove duplicatesAdditionally, we can use console.table()
to show the result nicely:
console.table([...[...$$("[class]")].reduce((s, e) =>
(e.classList.forEach(c => s.add(c)), s), new Set())].sort())