You can easily traverse sideways in the DOM tree using jQuery to find siblings of an element. The jQuery sibling elements are those elements that share the same parent.
Traversing Sideways in DOM Tree:
Following are the jQuery Methods that are used to traverse sideways in the DOM tree:
siblings()
next()
nextAll()
nextUntil()
prev()
prevAll()
prevUntil()
jQuery siblings() Method:
The siblings() method in jQuery is used to get the sibling elements of the selected element.
In the following example, the siblings of the <p> element which is <h2> and <ul> will be highlighted by adding the class .highlight-sibling on document ready.
You can also include an optional parameter to filter your search for the siblings.
In the following example, the border will be applied around the siblings of the <p> i.e to the <ul> elements.
The next() method in jQuery is used to get the immediately following sibling i.e. the next sibling element of the selected element. In the following example, the next sibling of the <p> element which is the <ul> will be highlighted.
We use cookies to ensure that we give you the best experience on our website. If you continue to use this site we will assume that you are happy with it.Ok