over 9 years ago
Hello Readers,
If we want to fetch or return the all sibling elements of the selected or matched elements then we use the .siblings() method in jQuery.
It is the elements that belong to the same parent.
Syntax :
Parameters :
selector - selector is a optional parameter which is used to filter the sibling elements.
Below is the Code Example of siblings method :
- <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 type = "text/javascript" language = "javascript">
- $(document).ready(function(){
- $("p").siblings('.selected').addClass("hilight");
- });
- </script>
- <style>
- .hilight { background:yellow; }
- </style>
- </head>
- <body>
- <div><span>Hello</span></div>
- <p class = "selected">Hello Again</p>
- <p>And Again</p>
- </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 type = "text/javascript" language = "javascript"> $(document).ready(function(){ $("p").siblings('.selected').addClass("hilight"); }); </script> <style> .hilight { background:yellow; } </style> </head> <body> <div><span>Hello</span></div> <p class = "selected">Hello Again</p> <p>And Again</p> </body> </html>
In the above Code, we use the optional parameter class (.selected) and addClass (hilight) on it.
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)