Skip to content
Related Articles

Related Articles

jQuery | addBack() with Examples
  • Last Updated : 20 Aug, 2018

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 element 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:

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




<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="https://code.jquery.com/jquery-1.10.2.js"></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>

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.
Output:

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




<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="https://code.jquery.com/jquery-1.10.2.js">
     </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:




My Personal Notes arrow_drop_up
Recommended Articles
Page :