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
    • 354
    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 :

    1. selector.andSelf()

    Code Example :

    1. <html>
    2.  
    3. <head>
    4. <title>The jQuery Example</title>
    5. <script type = "text/javascript"
    6. src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    7.  
    8. <script>
    9. $(document).ready(function(){
    10. $("div").find("p").andSelf().addClass("border");
    11. });
    12. </script>
    13.  
    14. <style>
    15. p, div { margin:5px; padding:5px; }
    16. .border { border: 2px solid red; }
    17. .background { background:yellow; }
    18. </style>
    19. </head>
    20.  
    21. <body>
    22.  
    23. <div>
    24. <p>First Paragraph</p>
    25. <p>Second Paragraph</p>
    26. </div>
    27.  
    28. </body>
    29.  
    30. </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
Reset Password
Fill out the form below and reset your password: