Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is .andSelf() method in jquery ?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 351
    Comment on it

    Hello Readers,

    .andSelf() is the jquery method and it is the alias of .addBack() method because it is deprecated in jquery version 1.8. So, it's replacement is .addBack() method. .addSelf() method is used to add the previous selection to the current selection. This method is used without passing any argument.

    Syntax :

    selector.andSelf()
    

    Code Example :

    <html>
    
       <head>
          <title>The jQuery Example</title>
          <script type = "text/javascript" 
             src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    
          <script>
             $(document).ready(function(){
                $("div").find("p").andSelf().addClass("border");
             });
          </script>
    
          <style>
             p, div { margin:5px; padding:5px; }
             .border { border: 2px solid red; }
             .background { background:yellow; }
          </style>  
       </head>
    
       <body>
    
          <div>
             <p>First Paragraph</p>
             <p>Second Paragraph</p>
          </div>
    
       </body>
    
    </html>
    

    In the above code use the find(), andSelf() and addClass() method to find p paragraph and add the selection to the previous which is a div and then current selection which is a paragraph.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: