Reputation: 3
Is there a way to get elements with a class with a CSS attribute selector?
Something like this:
[class.=className]
Upvotes: 0
Views: 458
Reputation: 799
From MDN:
[attr~=value]
Represents elements with an attribute name of attr whose value is a whitespace-separated list of words, one of which is exactly value.
So this:
[class~=className]
targets all elements that has the class "className" regardless of whether is has other classes around it.
Upvotes: 2
Reputation: 44
You could use this selector:
[class="className"], [class^="className "], [class$=" className"], [class*=" className "]
It's a bit long because we need to check whether it's just that class, whether it's at the beginning of the class
attribute, the end, or in the middle.
Upvotes: 1