In jQuery, an inbuilt method andself() is defined to add the previous set of elements to the current set. It will keeps the previous elements to the current set and maintain it in the internal stack. That the internal stack will take responsible of changes to the matched set of elements.

Syntax of andself() Method

andself() (selector)

Here, it doesn’t accept any parameter and it returns all added element against the specified selector.

Example Script

<script>

        $(document).ready(function() {

            $(“div”).find(“p”).andSelf().addClass(“border”);

        });

</script>

In the latest version, it won’t work perfectly. Instead of using this, you can try addback() method. Here as follows;

Example of jQuery addback()
<head>
  <meta charset="utf-8">
  <title>addBack demo</title>
  <style>
  p, div {
    margin: 5px;
    padding: 5px;
  }
  .border {
    border: 2px solid red;
  }
  .background {
    background: yellow;
  }
  .left, .right {
    width: 45%;
    float: left;
  }
  .right {
    margin-left: 3%;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>
<body>
<div class="left">
  <p><strong>Before <code>addBack()

 
   

First Paragraph

   

Second Paragraph

 

After addBack()

 
   

First Paragraph

   

Second Paragraph

 

Leave a Reply

Your email address will not be published. Required fields are marked *