jQuery addClass() Method
The addClass() method is an inbuilt method in jQuery which is used to add more properties to each selected element. It can also be used to change the property of the selected element.
This method can be used in two different ways:
1) By adding a Class name directly: Here, the Class name can be used directly with the element which is going to be selected.
Parameters: It accepts the parameter “className” which is the name of the class that is going to be added.
Return Value: It returns the selected elements with the added new class.
In the above code, “p” element is selected and “selected” class is applied only on last “p” element with the help of .last() method and .addClass() method of jQuery.
2) By passing a function to add a new class: Here, A function can be passed to the .addClass() for the selected element.
Parameters: It accepts a parameter “function”.
Return Value: It returns the selected element with added function.
In the above code, the “div” element is selected and with the help of a function, a new class is added to the selected div element.