Skip to content
Related Articles

Related Articles

Improve Article

jQuery | add() method with Example

  • Last Updated : 13 Feb, 2019
Geek Week

The jQuery add() method is used to add element to the existing group of elements. This method can add element to the whole document, or just inside context element if the context parameter is defined.
Syntax:

$(selector).add(element, context_parameter)

Here selector helps to find the matching element.
Parameters: It accept two parameters that are specified below:

  • element: It is the selected element.
  • context_parameter: It is the context parameter of the element defined.



    jQuery code to show the working of add() method:




    <html>
      
    <head>
                     /jquery/3.3.1/jquery.min.js"></script>
        <script>
            $(document).ready(function() {
                $(".heading").add("#para1").add("#span1").
                css("background-color", "lightgreen");
            });
        </script>
    </head>
      
    <body>
        <p style="color:green" class="heading">Welcome to GfG!!!</p>
        <p style="color:green" id="para1">Welcome to GeeksforGeeks !!!</p>
        <span style="color:green" id="span1">Article 1 !!!.</span>
        <div>This example adds the same css style for both
            "p" and "span" elements, using its class and
             id name!!! </div>
    </body>
      
    </html>

    Output:

    Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it’s lowest price ever!

    My Personal Notes arrow_drop_up
  • Recommended Articles
    Page :