I want to select all the elements that have the two classes a
and b
.
<element class="a b">
So, only the elements that have both classes.
When I use $(".a, .b")
it gives me the union, but I want the intersection.
csshtmlhtmlelementsjqueryjquery-selectors
I want to select all the elements that have the two classes a
and b
.
<element class="a b">
So, only the elements that have both classes.
When I use $(".a, .b")
it gives me the union, but I want the intersection.
Best Answer
If you want to match only elements with both classes (an intersection, like a logical AND), just write the selectors together without spaces in between:
The order is not relevant, so you can also swap the classes:
So to match a
div
element that has an ID ofa
with classesb
andc
, you would write:(In practice, you most likely don't need to get that specific, and an ID or class selector by itself is usually enough:
$('#a')
.)