jQuery Siblings
Siblings are components that share the same parent in logical connections. To move sideways in the DOM tree, jQuery offers numerous methods such as siblings(), next(), nextAll(), nextUntil(), prev(), prevAll(), and prevUntil().
jQuery siblings() Method
To retrieve the sister elements of the specified element, use the jQuery siblings() function. The following example uses the class to highlight the siblings of the <p>
element, which are <h1>
and <ul>
. highlight on document ready.
You can optionally pass one or more selectors as parameters to the siblings() function to narrow down your search for siblings. The border surrounding the siblings of the <p>
that are <ul>
elements will only be applied in the following example.
jQuery next() Method
The jQuery next() function is used to fetch the element's immediately following sibling, i.e. the next sibling element. The <ul>
element is the <p>
element's next sibling.
jQuery nextAll() Method
The jQuery nextAll() function returns all siblings of the specified element. The example below will highlight all siblings of the <p>
element that comes after it.
jQuery nextUntil() Method
The jQuery nextUntil() function returns all siblings up to but excluding the element matched by the selection. In a nutshell, it retrieves all the next siblings items in a DOM hierarchy between two supplied components. The following example will highlight all of the <h1>
element's siblings except the <ul>
element, i.e. highlight both the <p>
element.
jQuery prev() Method
The jQuery prev() function is used to acquire the chosen element's immediately preceding sibling, i.e. the previous sibling element. The preceding sibling of the <ul>
element, the <p>
element.
jQuery prevAll() Method
The jQuery prevAll() function returns all siblings of the specified element. The example below will highlight all siblings of the <ul>
element that comes before this.
jQuery prevUntil() Method
The jQuery prevUntil() function returns all siblings preceding but not including the element matched by the selector. In a nutshell, it retrieves all the preceding siblings items in a DOM hierarchy between two supplied components. The following example will highlight all of the <ul>
element's prior sibling elements except the <h1>
element, highlighting both the <p>
element.