HTML tutorial
CSS3 tutorial
Bootstrap tutorial
JavaScript tutorial
JQuery tutorial
AngularJS tutorial
React tutorial
NodeJS tutorial
PHP tutorial
Python tutorial
Python3 tutorial
Django tutorial
Linux tutorial
Docker tutorial
Ruby tutorial
Java tutorial
C tutorial
C ++ tutorial
Perl tutorial
JSP tutorial
Lua tutorial
Scala tutorial
Go tutorial
ASP.NET tutorial
C # tutorial
With jQuery you can traverse sideways in the DOM tree to find siblings of an element.Siblings share the same parent.
There are many useful jQuery methods for traversing sideways in the DOM tree:
siblings()
next()
nextAll()
nextUntil()
prev()
prevAll()
prevUntil()
The 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");
});
The 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();
});
The 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();
});
The 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");
});
The prev()
, prevAll()
and prevUntil()
methods work just like the methods above but with reverse functionality: they
return previous sibling elements (traverse backwards along sibling
elements in the DOM tree, instead of forward).
Use a jQuery method to get all siblings elements of an <h2> element.
$("h2").();
For a complete overview of all jQuery Traversing methods, please go to our jQuery Traversing Reference.