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:

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for Class Selector (“.class”) below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • 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");