jQuery CSS Classes

tutorial and example
tutorial and example

Using the jQuery CSS classes, it is easy to manipulate the CSS of elements.

jQuery has several methods for CSS manipulation.

These are as follows:

jQuery addClass() Method

jQuery addClass() method is used to add class attributes to different elements. This method is only used to add the class name to the class elements not to remove the attributes of the existing class.

Syntax:

classname: It is used to specify one or more class names which you want to add and it is a mandatory parameter.

function: It is used to specify a function that returns one or more class names to be added and it is an optional parameter.

index: It is used to return the index position of the content.

currentclass: It is used to provide the current class name of the selected content.

Example:

Try Now

jQuery removeClass() Method

jQuery remove() method is used to remove one or more classes from the selected elements.

Syntax:

classname: It is used to specify one or more class names which you want to remove and it is a mandatory parameter.

function: It is used to specify a function that returns one or more class names to be removed and it is an optional parameter.

index: It is used to return the index position of the content.

currentclass: It is used to provide the current class name of the selected content.

Example:

Try Now

jQuery toggleClass()

Using the jQuery toggleClass() method you can add or remove the classes from the selected elements. In this method, if the name of the class is missing, it adds or if the class name is already set, it removes.

Syntax

classname: It is used to specify one or more class names which you want to add or remove. It is a mandatory parameter.

function: It is used to specify a function that returns one or more class names to be added or removed. It is an optional parameter.

index: It is used to return the index position of the content.

currentclass: It is used to provide the current class name of the selected content.

Example:

Try Now