The Wayback Machine - https://web.archive.org/web/20240927010400/https://www.geeksforgeeks.org/jquery-addclass-method/
Open In App

jQuery addClass() Method

Last Updated : 11 Jul, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The addClass() method is an inbuilt method in jQuery that 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. 

Syntax:

$(selector).addClass(className);

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.

Example 1: In this example, we are using the above-explained method.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <meta charset="utf-8">
    <script src=
    </script>
    <title>addClass demo</title>
    <style>
        p {
            margin: 8px;
            font-size: 35px;
        }
 
        .selected {
            display: block;
            border: 2px solid green;
            width: 160px;
            height: 60px;
            background-color: lightgreen;
            padding: 20px;
        }
 
        .highlight {
            background: yellow;
        }
    </style>
</head>
 
<body>
    <p>GeeksforGeeks</p>
    <p>gfg</p>
    <p>CSE</p>
    <script>
        $("p").last().addClass("selected");
    </script>
</body>
 
</html>


In the above code, “p” element is selected, and the “selected” class is applied only to the last “p” element with the help of .last() method and .addClass() method of jQuery. 

Output:

  

2) By passing a function to add a new class: Here, A function can be passed to the .addClass() for the selected element. 

Syntax:

$(selector).addClass(function);

Parameters: It accepts a parameter “function”. 

Return Value: It returns the selected element with added function. 

Example 2:  Here is the example of the above-explained method.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <meta charset="utf-8">
    <script src=
    </script>
    <style>
        div {
            background: white;
            margin: 20px;
        }
 
        .red {
            background: red;
            width: 300px;
            margin: 20px;
        }
 
        .red.green {
            background: lightgreen;
            margin: 20px;
            border: 2px solid green;
        }
 
        body {
            border: 2px solid green;
            width: 350px;
            height: 200px;
        }
    </style>
</head>
 
<body>
    <div>This div should be white</div>
    <div class="red">
        This div will be green because now it
        has the both "green" and "red" classes.
    </div>
    <div>This div should be white</div>
 
    <script>
        $("div").addClass(function (index, currentClass) {
            let addedClass;
            if (currentClass === "red") {
                addedClass = "green";
                $("p").text("There is one green div");
            }
            return addedClass;
        });
    </script>
</body>
 
</html>


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. 

Output: 



Similar Reads

jQuery UI | addClass() Method
The addClass() method is an inbuilt method in jQuery UI framework which is used to manage user interface visual effects. This method adds class to all selected elements along with animating all the defined styles in CSS properties. It basically manages animation methods for text-indentation, width, height, padding, margins, font-sizes and letter sp
6 min read
How to add class to an element without addClass() method in jQuery ?
The task is to add a new class to the element without using addClass() method with the help of jQuery. There are two approaches that are discussed below: Approach 1: To perform the operation, we can use toggleClass() method to toggle the class which we want to add to the element. Example: [GFGTABS] HTML <!DOCTYPE HTML> <html> <head
2 min read
How to animate jQuery addClass/removeClass functions ?
In this article, we will learn to animate using jQuery addClass() and removeClass() functions. We are going to use jQuery and jQuery UI. jQuery UI (User Interface) is free and open-source software that is built on top of the core powerful jQuery library. If you want to use jQuery UI, you must include jQuery too. jQuery UI provides more features lik
2 min read
Fabric.js addClass() Method
The addClass() method in Fabric.js is used to add the specified class to the specified HTML element. The HTML element in the page has to be selected first to pass this method. Syntax: addClass(element, className) Parameters: This method accept two parameters as mentioned above and described below: element: This parameter is used to specify the HTML
2 min read
p5.Element addClass() Method
The addClass() method of p5.Element in p5.js is used to add the specified class to an element. An element can have multiple classes assigned to it. Also, one class can be specified to multiple elements on the page. Syntax: addClass(class)Parameters: This function accepts a single parameter as mentioned above and described below. class: It is a stri
2 min read
jQuery Cheat Sheet – A Basic Guide to jQuery
What is jQuery?jQuery is an open-source, feature-rich JavaScript library, designed to simplify the HTML document traversal and manipulation, event handling, animation, and Ajax with an easy-to-use API that supports the multiple browsers. It makes the easy interaction between the HTML & CSS document, Document Object Model (DOM), and JavaScript.
15+ min read
jQuery jQuery.fx.interval Property with example
The jQuery.fx.interval property in jQuery is used to modify the number of frames per second at which animations will run and to change the animation firing rate in milliseconds. Its default value is 13ms. Syntax: jQuery.fx.interval = milliseconds;Parameters: This method accepts single parameter milliseconds which is mandatory. It is used to specify
2 min read
jQuery jQuery.fx.off Property
The jQuery.fx.off property in jQuery is used to globally disable/enable all animations. Its default value is false which is used to allow animation to run normally. Syntax: jQuery.fx.off = true|false;Parameter: This event accepts two parameters as mentioned above and described below: true: It is used to specify that the animations should be disable
2 min read
jQuery jQuery.support Property
The jQuery.support property in jQuery contains a collection of properties that are used to represent the different browser features or bugs. Syntax: jQuery.support.propvalueParameters: This property contains a single parameter propvalue: which is required. It is used to specify the function to test for. The tests included are: ajaxboxModelchangeBub
1 min read
jQuery jquery Property
The jquery property is used to return the jQuery version number. Syntax $().jqueryExample: Return the version number. C/C++ Code <!DOCTYPE html> <html> <head> <style> h1 { color: green; } </style> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script
1 min read
How to append a jQuery object to all paragraphs using jQuery ?
In this article, we will see how to append a jQuery object to all paragraphs using jQuery. Append means we add something to an existing element. The object is used to define a container for an external resource like a page, a picture, a media player, or a plug-in application. Used Methods: ready() Method: This method is used to specify a function t
2 min read
How to insert a jQuery object after all paragraphs using jQuery ?
The task is to insert a jQuery object after all the paragraphs using jQuery. Approach: Create DOM element or jQuery object using <span> tag.Create a paragraph using <p> tag.Create a button using button tag after clicking on which the object will be inserted after paragraph.Write a script that will call a function jQuery.after() which is
1 min read
Difference between jquery.size() and jquery.length
JQuery.size() method gives us the number of elements present. For Example, if we are calling the size() method for "p" tag, then it will return the number of "p" tags present on our page. Syntax: $(selector).size() Return value: It returns the number of “selector” present. Example: C/C++ Code <!DOCTYPE html> <html lang="en">
2 min read
How to remove close button from jQuery UI dialog using jQuery ?
In this article, we will learn how to remove the close button on the jQuery UI dialog using JavaScript, This can be achieved using the hide() method. jQuery UI is a curated set of user interface interactions, effects, widgets, and themes built on top of the jQuery JavaScript Library. A dialog box is a temporary window. An application creates a dial
2 min read
How is the deferred method in jquery important in relation to animate method ?
jQuery is a popular JavaScript library that provides a number of useful methods for manipulating the DOM (Document Object Model). One of these methods is the animate method, which allows you to animate the properties of an element over a given period of time. The animate() method is often used in conjunction with the Deferred() method, which allows
4 min read
jQuery Mobile Collapsibleset refresh() Method
jQuery Mobile is a web-based technology that can be used to make responsive content for websites that can be accessed on all types of smartphones, tablets, and desktops and it is also used by many users. In this article, we will be using the jQuery Mobile Collapsibleset refresh() method to update the collapsibleset widget. This method is used to up
2 min read
jQuery UI Sortable widget() Method
jQuery UI is a web-based technology and consists of GUI widgets, visual effects, and themes implemented using the jQuery, JavaScript Library. jQuery UI is the best tool for building UI interfaces for the webpages. It can also be used to build highly interactive web applications or can be used to add widgets easily. In this article, we will be using
2 min read
jQuery prepend() Method
The prepend() method is an inbuilt method in jQuery that is used to insert specified content at the beginning of the selected element. Syntax: $(selector).prepend(content, function)Parameters: This method accepts two parameters as mentioned above and described below: content: It is a required parameter that is used to specify the content that needs
2 min read
jQuery offset() Method
The offset() method is an inbuilt method in jQuery which is used to set or returns the offset coordinates of the selected element.Syntax: $(selector).offset()Parameters: Parameter does not required.$(selector).offset({top:value, left:value})Parameters: The parameter is required when set the offset.$(selector).offset( function(index, offset) )Parame
2 min read
jQuery UI Tooltip widget() Method
jQuery UI consists of GUI widgets, visual effects, and themes implemented using jQuery, CSS, and HTML. jQuery UI is great for building UI interfaces for the webpages. jQuery UI tooltip widget helps us to add new themes and allows for customization. In this article, we will see how to use widget method in jQuery UI tooltips. The widget method is use
1 min read
jQuery UI | position() Method
The jQuery UI framework provides many utility functions to the user, one of which is position() method. The position() method helps in positioning any element relative to any target element in the page like window, any parent element, document or mouse. It describes the position of any element based on properties like "top", "left", "right" and "bo
4 min read
JQuery | parseHTML() method
This parseHTML() Method in jQuery is used to parses a string into an array of DOM nodes. Syntax: jQuery.parseHTML(data [, context ] [, keepScripts ]) Parameters: The parseXML() method accepts three parameter that is mentioned above and described below: data: This parameter is the HTML string to be parsed. context : This parameter is the document el
2 min read
jQuery UI Autocomplete search() Method
jQuery UI consists of GUI widgets, visual effects, and themes implemented using HTML, CSS, and jQuery. jQuery UI is great for building UI interfaces for the webpages. The jQuery UI Autocomplete widget is used to perform autocomplete enables to quickly find and select from a pre-populated list of values as they type, leveraging searching and filteri
2 min read
jQuery focusin() Method
The jQuery focusin() is an inbuilt method that is used to gain focus on the selected element. Syntax: $(selector).focusin(function);Parameter: It accepts an optional parameter "function" which gain focus on the selected element. jQuery examples to show the working of focusin() method: Example 1: In the below code, parameter function is passed. C/C+
2 min read
jQuery css() Method
The css() method in jQuery is used to change the style property of the selected element. This method can be used in different ways. The css() method can be used to get/return the present value of the property for the selected element. Syntax: $(selector).css(property)or $(selector).css(property, value)or $(selector).css(property, function(index, cu
2 min read
jQuery remove() Method
The remove() method in JQuery is used to remove all the selected elements including all the text. This method also remove data and all the events of the selected elements. Syntax: $(selector).remove()Return Value: It will return all the data of the selected elements deleted. Example 1: Input: $("p").remove()Output: Output will be all the elements o
2 min read
jQuery width() Method
The width() is an inbuilt function in JavaScript which is used to check the width of an element. It does not check the padding, border, and margin of the element. Syntax: $("param").width()Parameters: Here parameter is "param" which is the class or id of the element whose width is to be extracted. Return values: It returns the width of the selected
2 min read
jQuery filter() Method
The jQuery is a very powerful tool which helps us to incorporate a variety of DOM traversal methods to select elements in a document randomly or in sequential order. Most of the DOM traversal methods do not modify the elements whereas they filter them out upon the given conditions. The filter() method is used to filter out all the elements that do
2 min read
jQuery eq() Method
The eq() method is an inbuilt method in jQuery that is used to locate the selected elements directly and returns an element with a specific index. Syntax: $(selector).eq(index)Parameters: Here the parameter "index" specifies the index of the element.Can either be positive or a negative number. NOTE: The index number always starts at 0, so the first
2 min read
jQuery empty() Method
The empty() method is an inbuilt method in jQuery that is used to remove all child nodes and their content for the selected elements. Syntax: $(selector).empty()Parameter: This method does not accept any parameter. Return Value: This method returns the selected element with the specified changes made by the empty() method. The below example illustr
1 min read