Gal
Gal

Reputation: 5416

Select elements with a single class in JQuery

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

Upvotes: 8

Views: 3752

Answers (1)

zerkms
zerkms

Reputation: 254926

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

Upvotes: 14

Related Questions