Introduction to jQuery
JQuery Syntax
JQuery Effects
JQuery HTML
JQuery Traversing
In jQuery, you can easily get, add, remove, or toggle CSS classes using built-in methods. These are useful for dynamically changing the look and behavior of elements.
|
Method |
Description |
|---|---|
|
.addClass() |
Adds one or more classes to selected elements |
|
.removeClass() |
Removes one or more classes from selected elements |
|
.toggleClass() |
Adds/removes a class based on its current state |
|
.hasClass() |
Checks if the selected element has a specific class |
The following stylesheet will be used for all the examples on this page:
.highlight {
background-color: yellow;
font-weight: bold;
}
.blue {
color: blue;
}
he following example shows how to add class attributes to different elements. Of course you can select multiple elements, when adding classes:
Try it yourself
You can also specify multiple classes within the addClass() method:
Try it yourself
The following example shows how to remove a specific class attribute from different elements:
Try it yourself
The following example will show how to use the jQuery toggleClass() method. This method toggles between adding/removing classes from the selected elements:
Try it yourself