How can I select an element with multiple classes in jQuery?

HtmlJqueryCssJquery SelectorsHtmlelements

Html Problem Overview


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.

Html Solutions


Solution 1 - Html

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:

$('.a.b')

The order is not relevant, so you can also swap the classes:

$('.b.a')

So to match a div element that has an ID of a with classes b and c, you would write:

$('div#a.b.c')

(In practice, you most likely don't need to get that specific, and an ID or class selector by itself is usually enough: $('#a').)

Solution 2 - Html

You can do this using the filter() function:

$(".a").filter(".b")

Solution 3 - Html

For the case

<element class="a">
  <element class="b c">
  </element>
</element>

You would need to put a space in between .a and .b.c

$('.a .b.c')

Solution 4 - Html

The problem you're having, is that you are using a Group Selector, whereas you should be using a Multiples selector! To be more specific, you're using $('.a, .b') whereas you should be using $('.a.b').

For more information, see the overview of the different ways to combine selectors herebelow!


##Group Selector : ","

Select all <h1> elements AND all <p> elements AND all <a> elements :

$('h1, p, a')

##Multiples selector : "" (no character)

Select all <input> elements of type text, with classes code and red :

$('input[type="text"].code.red')

##Descendant Selector : " " (space)

Select all <i> elements inside <p> elements:

$('p i')

##Child Selector : ">"

Select all <ul> elements that are immediate children of a <li> element:

$('li > ul')

##Adjacent Sibling Selector : "+"

Select all <a> elements that are placed immediately after <h2> elements:

$('h2 + a')

##General Sibling Selector : "~"

Select all <span> elements that are siblings of <div> elements:

$('div ~ span')

Solution 5 - Html

$('.a .b , .a .c').css('border', '2px solid yellow');
//selects b and c

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="a">a
  <div class="b">b</div>
  <div class="c">c</div>
  <div class="d">d</div>
</div>

Solution 6 - Html

Just mention another case with element:

E.g. <div id="title1" class="A B C">

Just type: $("div#title1.A.B.C")

Solution 7 - Html

Vanilla JavaScript solution:-

document.querySelectorAll('.a.b')

Solution 8 - Html

For better performance you can use

$('div.a.b')

This will look only through the div elements instead of stepping through all the html elements that you have on your page.

Solution 9 - Html

your code $(".a, .b") will work for below multiple elements (at a same time)

<element class="a">
<element class="b">

if you want to select element having a and b both class like <element class="a b"> than use js without comma

$('.a.b')

Solution 10 - Html

Group Selector

body {font-size: 12px; }
body {font-family: arial, helvetica, sans-serif;}
th {font-size: 12px; font-family: arial, helvetica, sans-serif;}
td {font-size: 12px; font-family: arial, helvetica, sans-serif;}

Becomes this:

body, th, td {font-size: 12px; font-family: arial, helvetica, sans-serif;}

So in your case you have tried the group selector whereas its an intersection

$(".a, .b") 

instead use this

$(".a.b") 

Solution 11 - Html

You do not need jQuery for this

In Vanilla you can do :

document.querySelectorAll('.a.b')

Solution 12 - Html

You can use getElementsByClassName() method for what you want.

var elems = document.getElementsByClassName("a b c");
elems[0].style.color = "green";
console.log(elems[0]);

<ul>
  <li class="a">a</li>
  <li class="a b">a, b</li>
  <li class="a b c">a, b, c</li>
</ul>

This is the fastest solution also. you can see a benchmark about that here.

Solution 13 - Html

Below example will give you idea about to select at a time multiple nested class selectors and direct class selectors in one line

//Here is Explaination of Selectors  
//.a .b .c  = selects nested child c which is inside of div a and b
//.a .d     = selects nested child d which is inside of div a 
//.f        = selects direct element ie div f which is outside of div a and b
$('.a .b .c , .a .d, .f').css('background-color', 'grey');

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<div class="a">a
  <div class="b">b
       <div class="c">c</div>
  </div> 
  <div class="d">d</div>
</div>
<div class="e">e</div>
<div class="f">f</div>

Solution 14 - Html

var elem = document.querySelector(".a.b");

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMladenView Question on Stackoverflow
Solution 1 - HtmlSasha ChedygovView Answer on Stackoverflow
Solution 2 - HtmlJamie LoveView Answer on Stackoverflow
Solution 3 - HtmljuanpauloView Answer on Stackoverflow
Solution 4 - HtmlJohn SlegersView Answer on Stackoverflow
Solution 5 - Htmluser2298171View Answer on Stackoverflow
Solution 6 - Htmlmacio.JunView Answer on Stackoverflow
Solution 7 - HtmlSalman von AbbasView Answer on Stackoverflow
Solution 8 - HtmlTejas Anil ShahView Answer on Stackoverflow
Solution 9 - HtmlSamView Answer on Stackoverflow
Solution 10 - HtmlSurya R PraveenView Answer on Stackoverflow
Solution 11 - HtmlSandwellView Answer on Stackoverflow
Solution 12 - Htmlbahman parsamaneshView Answer on Stackoverflow
Solution 13 - HtmlGauri BhosleView Answer on Stackoverflow
Solution 14 - HtmlKarim AliView Answer on Stackoverflow