Open In App

jQuery addBack() Method

Last Updated : 11 Jul, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

The addBack() is an inbuilt method in jQuery that adds the previous set of elements to the current set. This method adds previous DOM tree elements to the current set and maintains them in the internal stack which will take care of changes to the matched set of elements.

Syntax:

.addBack(selector)

Parameters: It accepts an optional parameter “selector”.

Return Value:

  • It returns the added element against the specified selector.

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

Example 1: In the below code, “p” element is passed as a parameter.

html




<!DOCTYPE html>
<html>
 
<head>
    <meta charset="utf-8">
    <style>
        #h {
            border: 2px solid white;
        }
 
        p,
        div {
            margin: 5px;
            padding: 5px;
            border: 2px solid green;
        }
 
        .border {
            border: 2px solid red;
        }
 
        .background {
            background: lightgrey;
        }
 
        .left,
        .right {
            width: 45%;
            float: left;
            border: 2px solid green;
        }
 
        .right {
            margin-left: 3%;
        }
    </style>
    <script src=
 
    </script>
</head>
 
<body>
 
    <div class="left">
        <p id="h">
            <strong>Before
                <code>addBack()</code>
            </strong>
        </p>
        <div class="before-addback">
            <p>First Paragraph</p>
            <p>Second Paragraph</p>
        </div>
    </div>
    <div class="right">
        <p id="h">
            <strong>After
                <code>addBack()</code>
            </strong>
        </p>
        <div class="after-addback">
            <p>First Paragraph</p>
            <p>Second Paragraph</p>
        </div>
    </div>
 
    <script>
        $(".before-addback").find("p").addClass(
            "background");
        $(".after-addback").addBack("p").addClass(
            "background");
    </script>
</body>
 
</html>


Output: In the above example, first “p” element is highlighted then after using .addBack() method next “div” element is adding to the stack after the “p” element.

Example 2: In the below code, no parameter is passed to the .addBack() method.

html




<!DOCTYPE html>
<html>
 
<head>
    <style>
        p,
        div {
            margin: 5px;
            padding: 5px;
        }
 
        div {
            border: 2px solid green;
        }
 
        .border {
            border: 2px solid green;
        }
 
        .background {
            background: lightgreen;
            border: 2px solid green;
        }
 
        .left,
        .right {
            width: 45%;
            float: left;
        }
 
        .right {
            margin-left: 3%;
        }
    </style>
    <script src=
    </script>
</head>
 
<body>
    <div class="left">
        <p>
            <strong>Before
                <code>addBack()</code>
            </strong>
        </p>
        <div class="before">
            <p>First Paragraph</p>
            <p>Second Paragraph</p>
        </div>
    </div>
    <div class="right">
        <p>
            <strong>After
                <code>addBack()</code>
            </strong>
        </p>
        <div class="after">
            <p>First Paragraph</p>
            <p>Second Paragraph</p>
        </div>
    </div>
    <script>
        $(".before").find("p").addClass(
            "background");
        $(".after").find("p").addBack().addClass(
            "background");
    </script>
</body>
 
</html>


Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads