jQuery | addClass() with Examples
The addClass is an inbuilt method in jQuery which is used to add more property 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 Class name directly:
Here, Class name can be used directly with the element which are going to be selected.
Parameters: It accepts a parameter “className” which is the name of the class that are going to be added.
Return Value: It returns the selected elements with 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 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.