I want to be able to select all elements that only have a single (given) class (and no other)
for example, let's say my page looks like this
<html>
<body>
<div class="a"> </div>
<div class="a b"></div>
<div class="c"></div>
</body>
</html>
I want to be able to select only the first element - because it has a single class "a", and no other classes.
Thanks
In jQuery, the class and ID selectors are the same as in CSS. If you want to select elements with a certain class, use a dot ( . ) and the class name. If you want to select elements with a certain ID, use the hash symbol ( # ) and the ID name.
To select elements with a specific class, write a period (.) character, followed by the name of the class. You can also specify that only specific HTML elements should be affected by a class. To do this, start with the element name, then write the period (.)
The . class selector can also be used to select multiple classes. Note: Seperate each class with a comma. Note: Do not start a class attribute with a number.
How about this:
$('.a[class="a"]')
or as @Hexa propsed:
$('div[class="a"]')
http://jsfiddle.net/taYWP/
@Hexa: and it's actually the fastest! ;-) http://jsperf.com/attr-vs-attr-and-class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With