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

Recommended Posts:
- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.fx.off Property
- jQuery | jQuery.support Property
- jQuery | jquery Property
- jQuery | prepend() with Examples
- jQuery | dblclick() with Examples
- jQuery | offset() with Examples
- jQuery | children() with Examples
- jQuery | Hide/Show, Toggle and Fading methods with Examples
- jQuery | focusin() with Examples
- jQuery | Animation, Slide methods with Examples
- jQuery | change() with Examples
- jQuery | filter() with Examples
- jQuery | height() and innerHeight() with Examples
- jQuery | eq() with Examples
- jQuery | empty() with Examples
- jQuery | not() method with Examples
- jQuery | first() with Examples
- jQuery | parent() & parents() with Examples
- jQuery | noConflict() with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


