OpenJS Foundation - openjsf.org · api.jquery.com

class selector

Description: Selects all elements with the given class.

  • version added: 1.0jQuery( ".class" )

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

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

Examples:

Example 1

Finds the element with the class "myClass".

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>class demo</title>

<style>

div, span {

width: 120px;

height: 40px;

float: left;

padding: 10px;

margin: 10px;

background-color: #EEEEEE;

}

</style>

<script src="https://code.jquery.com/jquery-4.0.0.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 2

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

<!doctype html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>class demo</title>

<style>

div, span {

width: 120px;

height: 40px;

float: left;

padding: 10px;

margin: 10px;

background-color: #EEEEEE;

}

</style>

<script src="https://code.jquery.com/jquery-4.0.0.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:

Read the original on api.jquery.com ↗