What do you understand by jQuery Traversing Siblings?

jQueryWeb DevelopmentFront End Technology

jQuery traversing siblings  is traverse to find siblings of an elements using jQuery. To traverse sideways in DOM tree, use the following methods:

  • siblings(): This returns all the siblings elements of the selected element.
  • next(): This method returns the next sibling element of the selected element.
  • nextAll(): This method returns all next sibling element of the selected element.
  • nextUntil(): The nextUntil() method returns all next sibling elements between two given arguments.
  • prev():This method returns the previous sibling element of the selected element.
  • prevAll(): This method returns all previous sibling element of the selected element
  • prevUntil():The prevUntil() method returns all next previous sibling elements between two given arguments.

Let’s see the sibling method,

If you want to get the sibling elements, then use the jQuery siblings() method. It returns all the sibling elements of the selected element.

Example

You can try to run the following code to learn how to work with siblings() method in jQuery:

Live Demo

<!DOCTYPE html>
<html>
<head>
<style>
.myclass * {
    display: block;
    border: 2px solid green;
    color: green;
    padding: 3px;
    margin: 10px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("li.new").siblings().css({"color": "blue", "border": "2px solid yellow"});
});
</script>
</head>
<body>

<div style="width:600px;" class="myclass">
  <ol>ol
    <li>li</li>
    <li>li</li>
    <li class="new">li- This is the sibling.</li>
    <li>li</li>
    <li>li</li>
  </ol>  
</div>

</body>
</html>
raja
Published on 16-Dec-2017 23:33:22
Advertisements