Loading Please Wait...

Logo Lynxsia IT Solutions

jQuery Traversing - Siblings

JQ DOM Traversing - Siblings

With jQuery you can traverse sideways in the DOM tree to find siblings of an element. Siblings share the same parent.

Traversing Sideways in The DOM Tree

There are many useful jQuery methods for traversing sideways in the DOM tree:

  • siblings()
  • next()
  • nextAll()
  • nextUntil()
  • prev()
  • prevAll()
  • prevUntil()
jQuery siblings() Method

The jQuery siblings() method returns all sibling elements of the selected element.

The following example returns all sibling elements of <h2>:

					 
        
          $(document).ready(function(){
            $("h2").siblings();
          });
        
      

You can also use an optional parameter to filter the search for siblings.

The following example returns all sibling elements of <h2> that are <p> elements:

					 
        
          $(document).ready(function(){
            $("h2").siblings("p");
          });
        
      
jQuery next() Method

The jQuery next() method returns the next sibling element of the selected element.

The following example returns the next sibling of <h2>:

					 
        
          $(document).ready(function(){
            $("h2").next();
          });
        
      
jQuery nextAll() Method

The jQuery nextAll() method returns all next sibling elements of the selected element.

The following example returns all next sibling elements of <h2>:

					 
        
          $(document).ready(function(){
            $("h2").nextAll();
          });
        
      
jQuery nextUntil() Method

The jQuery nextUntil() method returns all next sibling elements between two given arguments.

The following example returns all sibling elements between a <h2> and a <h6> element:

					 
        
          $(document).ready(function(){
            $("h2").nextUntil("h6");
          });
        
      
jQuery prev() Method

The jQuery prev() method returns the previous sibling element of the selected element.

The following example returns the previous sibling of <h2>:

					 
        
          $(document).ready(function(){
            $("h2").prev();
          });
        
      
jQuery prevAll() Method

The jQuery prevAll() method returns all previous sibling elements of the selected element.

The following example returns all previous sibling elements of <h2>:

					 
        
          $(document).ready(function(){
            $("h2").prevAll();
          });
        
      
jQuery prevUntil() Method

The jQuery prevUntil() method returns all previous sibling elements between two given arguments.

The following example returns all sibling elements between a <h2> and a <h6> element:

					 
        
          $(document).ready(function(){
            $("h2").prevUntil("h6");
          });
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved