How to select an element with multiple classes using jQuery ? - GeeksforGeeks

20 Mar, 2020
There are two procedures to select an element with multiple classes by using jQuery. Both the procedures are described below with the proper example.
Using filter() Method: By using filter() method we can filter out all the elements that do not match the selected criteria and those matches will be returned.
Syntax:$(selector).filter(criteria, function(index))
$(selector).filter(criteria, function(index))
Example:<!DOCTYPE html> <html>   <head>     <title>         jQuery | Select an element        with multiple classes    </title>           <style>         h1 {             color: green;         }         body {             text-align: center;         }     </style>       <script src="https://code.jquery.com/jquery-1.12.4.min.js">    </script></head>   <body>     <h1>GeeksforGeeks</h1>      <h3>        Select an element with        multiple classes    </h3>       <div class="geeks">GeeksforGeeks</div>    <div class="geeks geeks1">jQuery</div>      <div class="geeks1">        Select an element with        multiple classes    </div>      <div class="geeks1 geeks">Using</div>    <div class="geeks geeks1 geeks2">        filter() method    </div>      <script>        $(document).ready(function(){            $(".geeks").filter(".geeks1").css(                "background-color", "yellow");                      $(".geeks.geeks2").filter(".geeks1")                .css("background-color", "green");        });    </script></body>   </html>
<!DOCTYPE html> <html>   <head>     <title>         jQuery | Select an element        with multiple classes    </title>           <style>         h1 {             color: green;         }         body {             text-align: center;         }     </style>       <script src="https://code.jquery.com/jquery-1.12.4.min.js">    </script></head>   <body>     <h1>GeeksforGeeks</h1>      <h3>        Select an element with        multiple classes    </h3>       <div class="geeks">GeeksforGeeks</div>    <div class="geeks geeks1">jQuery</div>      <div class="geeks1">        Select an element with        multiple classes    </div>      <div class="geeks1 geeks">Using</div>    <div class="geeks geeks1 geeks2">        filter() method    </div>      <script>        $(document).ready(function(){            $(".geeks").filter(".geeks1").css(                "background-color", "yellow");                      $(".geeks.geeks2").filter(".geeks1")                .css("background-color", "green");        });    </script></body>   </html>
Output:
Using .class Selector: By using .class Selector specifies the class for an element to be selected. It should not begin with a number. It gives styling to several HTML elements.
Syntax:$(".class1.class2.class3...")
$(".class1.class2.class3...")
Example:<!DOCTYPE html> <html>   <head>     <title>         jQuery | Select an element        with multiple classes    </title>           <style>         h1 {             color: green;         }         body {             text-align: center;         }     </style>           <script src="https://code.jquery.com/jquery-1.12.4.min.js">    </script></head>   <body>     <h1>GeeksforGeeks</h1>           <h3>         Select an element with        multiple classes    </h3>           <div class="geeks">GeeksforGeeks</div>    <div class="geeks geeks1">jQuery</div>          <div class="geeks1">        Select an element with        multiple classes    </div>          <div class="geeks1 geeks">Using</div>    <div class="geeks geeks1 geeks2">        .class Selector Method    </div>          <script>        $(document).ready(function(){            $(".geeks1").css(                "background-color", "yellow");                          $(".geeks").css(                "background-color", "white");              $(".geeks.geeks2").css(                "background-color", "green");        });    </script></body>   </html>
<!DOCTYPE html> <html>   <head>     <title>         jQuery | Select an element        with multiple classes    </title>           <style>         h1 {             color: green;         }         body {             text-align: center;         }     </style>           <script src="https://code.jquery.com/jquery-1.12.4.min.js">    </script></head>   <body>     <h1>GeeksforGeeks</h1>           <h3>         Select an element with        multiple classes    </h3>           <div class="geeks">GeeksforGeeks</div>    <div class="geeks geeks1">jQuery</div>          <div class="geeks1">        Select an element with        multiple classes    </div>          <div class="geeks1 geeks">Using</div>    <div class="geeks geeks1 geeks2">        .class Selector Method    </div>          <script>        $(document).ready(function(){            $(".geeks1").css(                "background-color", "yellow");                          $(".geeks").css(                "background-color", "white");              $(".geeks.geeks2").css(                "background-color", "green");        });    </script></body>   </html>
Output:
jQuery-Misc
JQuery
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
JQuery | Set the value of an input text field
How to change selected value of a drop-down list using jQuery?
Form validation using jQuery
How to change the background color after clicking the button in JavaScript ?
How to Dynamically Add/Remove Table Rows using jQuery ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills