about 9 years ago
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 :
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>
<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.
Can you help out the community by solving one of the following Javascript problems?
Do activity (Answer, Blog) > Earn Rep Points > Improve Rank > Get more opportunities to work and get paid!
For more topics, questions and answers, please visit the Tech Q&A page.
0 Comment(s)