Co-Authored By:

Asked by: Boutaina Vondenhagen
technology and computing web design and htmlWhat is the syntax to add class in jQuery?
Parameter | Description |
---|---|
function(index,currentclass) | Optional. Specifies a function that returns one or more class names to be added index - Returns the index position of the element in the set currentclass - Returns the current class name of the selected element |
Beside this, what is the use of addClass in jQuery?
jQuery addClass() The addclass() method is used to add one or more class name to the selected element. This method is used only to add one or more class names to the class attributes not to remove the existing class attributes. If you want to add more than one class separate the class names with spaces.
Thereof, how do I add a class to an element?
Adding the class name by using JavaScript can be done in many ways.
- Using . className property: This property is used to add a class name to the selected element. Syntax: element. className += "newClass";
- Using . add() method: This method is used to add a class name to the selected element. Syntax: element. classList.
$(this) is a jQuery wrapper around that element that enables usage of jQuery methods. jQuery calls the callback using apply() to bind this . Calling jQuery a second time (which is a mistake) on the result of $(this) returns an new jQuery object based on the same selector as the first one.