Suppose, I have three links,
<a class="a b">1</a>
<a class="a">1</a>
<a class="a">1</a>
Now i just want links which only have the class "a". If i use document.querySelectorAll('.a')
then i will get all three of the links.
I know i can use document.querySelectorAll('.a:not(.b)')
. but for that i need to know all the class name specifically. I just want to get the link which only had the class "a".
2
Answers
It might be a bit of a hack, but if the goal is to find the exact
class
value of"a"
then you can use an attribute selector:I would filter the original results by checking the length of the
classList
.