jQuery API

Class Selector (“.class”)

class selector

version added: 1.0jQuery('.class')

  • class
    A class to search for. An element can have multiple classes; only one of them must match.

Description: Selects all elements with the given class.

For class selectors, jQuery uses JavaScript's native getElementsByClassName() function if the browser supports it.

Examples:

Example: Finds the element with the class "myClass".

<!DOCTYPE html>
<html>
<head>
  <style>
  div,span {
    width: 100px;
    height: 40px;
    float:left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div class="notMe">div class="notMe"</div>

  <div class="myClass">div class="myClass"</div>
  <span class="myClass">span class="myClass"</span>
<script>$(".myClass").css("border","3px solid red");</script>

</body>
</html>

Demo:

Example: Finds the element with both "myclass" and "otherclass" classes.

<!DOCTYPE html>
<html>
<head>
  <style>
  div,span {
    width: 100px;
    height: 40px;
    float:left;
    padding: 10px;
    margin: 10px;
    background-color: #EEEEEE;
  }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div class="myclass">div class="notMe"</div>

  <div class="myclass otherclass">div class="myClass"</div>
  <span class="myclass otherclass">span class="myClass"</span>
<script>$(".myclass.otherclass").css("border","13px solid red");</script>

</body>
</html>

Demo:

Support and Contributions

Need help with Class Selector (“.class”) or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to Class Selector (“.class”)? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • Hessam R

    There is no direct implementation for 'without the class' (NOT HAS CLASS). In order to select elements without a specific class, you can use :not() selector in combination with .class selector:
    $(“.myclass:not(.otherclass)”).css(“border”,”13px solid red”);

  • http://www.overflow.biz Rodrigo

    .class selector will also get elements with multiple classes, eg:
    <input class='class1 class2 classN'>
    $(“.class2″) will work, however if you want to get only elements with that specific class,
    $(“[class=class2]“) will do it.

  • http://www.vividual.co.uk Jason

    Thanks Rodrigo! I tried your suggestion but also found that I needed to use an asterix like so:

    $(“[class*=class2]“)

  • odopod

    I think you would use something like :
    $('.myclass:eq(1)')

    http://api.jquery.com/eq-selector/

  • wolftron

    jQuery's class selector returns null for SVG nodes!

    See: http://stackoverflow.com/questions/3294553/jque…

  • Nader

    You can use this syntax to select elements with a certain class and NOT the other class:

    $(“[class*=class2] div:not(.class3)”)

    This will select elements that HAS class2 and NOT class3.

  • Sharky

    $(“.a.b”) seems to say, “class a and b”. Is there a way to say, “class a or b”?

  • Thomas Dausner

    please try $('.a, .b')

  • PANDYMiC

    simply enter a space between the two classes.

    $(“.a .b”)

    this will match all elements with class “a” OR class “b”

  • PANDYMiC

    simply enter a comma between the two classes in your selector.

    $(“.a, .b”)

    this will match all elements with class “a” OR class “b”

  • http://pulse.yahoo.com/_K7UFO7CFB3UVO6OYU74PG5FHME archie

    thanks for this tip!

    http://www.arthurabogadil.com

  • Icy32

    How to select element, which has only only one specific class and doesn't have any other ?

  • Initial

    How can I find a class on parent page from iframe?

  • nood

    $('.someclass[class="someclass"]) probably

  • nood

    $('.someclass[class="someclass"]') missed the closing tick

  • Davios

    I have a strange issue with the class selector. Since I updated with 1.4.4, all my class selectors suchas $(“.class”) do not work. I have to add a specific parent ID object to make them work: $(“#parentIDObject .class”)
    Did I miss something ? a default setting in jQuery ?

  • Utkarsh

    how to select a element(anchor tag) which is inside the cell of a grid(jqgrid)??

  • johan

    what is the escape character for dot(.).
    I have an html tag with an attribute class “blah.blah.blah baz.baz.baz”
    how do i state so that my selection would contain all those with class “blah.blah.blah” including the mentioned tag with attribute “blah.blah.blah baz.baz.baz” ?

  • johan

    use \

  • johan

    what is the escape character for dot(.).
    I have an html tag with an attribute class “blah.blah.blah baz.baz.baz”
    how do i state so that my selection would contain all those with class “blah.blah.blah” including the mentioned tag with attribute “blah.blah.blah baz.baz.baz” ?

  • Mark

    The *= will defeat the purpose pointed out by Rodrigo.

    $(“[class*=class2]“) will allow it to match <input class=”class1 class2″>. But this is unnecessary since $(“.class2″) will do the same.

    The whole point of doing $(“class=class2″) is to ensure that <input class=”class1 class2″> DOES NOT match, whereas <input class=”class2″> does.

  • hequ

    The escape character is \ so the selector would be $(“blah\.blah\.blah”). This selector also selects the elements even though the element would have multiple classes.

  • hequ

    sorry missed the dot in front, so the correct class selector is: $(“.blah\.blah\.blah”).