How to find all siblings of currently selected object in jQuery?

jQueryWeb DevelopmentFront End Technology

To find all siblings of currently selected object in jQuery, use the siblings() method. The siblings( [selector] ) method gets a set of elements containing all of the unique siblings of each of the matched set of elements.

Here is the description of all the parameters used by this method −

  • selector − This is optional selector to filter the sibling Elements with.

Example

You can try to run the following code to learn how to find all siblings:

Live Demo

<html>

   <head>
      <title>jQuery siblings() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function(){
            $("p").siblings('.selected').addClass("highlight");
         });
      </script>
       
      <style>
         .highlight {
            background:yellow;
         }
      </style>
   </head>
   
   <body>
   
      <div><span>Hello</span></div>
      <p class = "selected">Hello Again</p>
      <p>And Again</p>
       
   </body>
</html>
raja
Published on 16-Dec-2017 23:38:57
Advertisements