In web development, it is often required to find or select certain HTML elements in order to manipulate them. For the purpose of doing so you have traverse through the DOM tree and select a particular element. jQuery provides a huge list of methods that are used for traversing through the DOM tree. Here we have enlisted these jQuery methods.
- children() method
- each() method
- find() method
- first() method
- next() method
- parent () method
- prev() method
- siblings() method
These above-mentioned methods are explained in detail below.
children() Method
For the purpose of fetching all the immediate children of a specified element, the jQuery children() method is used. This method only traverses one level down the DOM tree.
Syntax
$(selector).children(filter)
Filter is an optional parameter that is used to specify an element to restrict the search for children.
Example
Suppose you want to fetch the direct children of a certain element using the jQuery children() method. Here is an example of doing so.
HTML
We have created a
- element is nested. Further inside the
- , and elements are nested.
CSS
.children * {
display: block;
border: 2px solid gray;
color: gray;
padding: 5px;
margin: 15px;
}
The entire body has been given some style by using CSS.
jQuery
$(document).ready(function(){
$(“ul”).children().css({“color”: “blue”, “border”: “2px solid blue”});
});
Here the children() method is used to fetch the direct children of
- element. Besides this, css() method is chained to the children() method in order to highlight the children of
- The function inside the .each() method is a required parameter function which will be executed for each of the specified elements.
- Index parameter specifies the position of the selector.
- Element parameter refers to the current element.
- and elements inside the
- element. The element further nests a element. Multiple nesting was done in order to make the working of the find() method clear.
- and elements in the
- element and later applying some css styling to highlight the fetched successors.
- element which further is a part of a
element.
HTML
There are a total of four elements generated in the above code;
,- ,
- , and . Observing their hierarchy, the
- element is regarded as a direct parent of the element,
- is the grandparent of the element, and
- elements inside the
- element.
- element. Moreover, in order to highlight those siblings we have used css() method.
Output
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image3-50.png" data-lazy- height="365" src="data:image/svg xml,” width=”632″>
The siblings() method is working properly.
Conclusion
jQuery provides a huge range of methods that are used for the purpose of traversing DOM elements. These methods are; children() method, each() method, find() method, first() method, next() method, parent() method, prev() method, and siblings() method. All of these methods serve their own purpose which is explained in-depth in this guide along with the help of relevant examples.
CSS
.div * {
display: block;
border: 2px solid gray;
color: gray;
padding: 5px;
margin: 15px;
}
The
element has been given a certain style using CSS.jQuery
Using the jQuery siblings() method we are going to fetch all the siblings of the
- element. Moreover, in order to highlight those siblings we have used css() method.
is the great-grandparent because all of the elements are nested inside theelement.CSS
.main * {
display: block;
border: 2px solid gray;
color: gray;
padding: 5px;
margin: 15px;
}
Using CSS, we have given some style to the body.
jQuery
We have applied the parent() method on the element and also chained the css() method to it in order to highlight the direct parent of the element and verify that the parent element is accessed successfully.
Output
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image2-54.png" data-lazy- height="312" src="data:image/svg xml,” width=”672″>
The parent element is accessed successfully.
prev() Method
The jQuery prev() method is used to fetch the previous adjacent sibling of a specified element. This method traverses in a backward manner in the DOM tree.
Syntax
Filter is an optional parameter that is used to specify an element to restrict the search for the previous adjacent sibling.
Example
Suppose you want to fetch the previous adjacent sibling of an element. Use the following code.
HTML
In the above code, we have created three elements which are;
,
, and
.
jQuery
We have used the jQuery prev() method to fetch the previous adjacent sibling of
element.
Output
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image1-52.png" data-lazy- height="188" src="data:image/svg xml,” width=”621″>
The previous adjacent sibling of
is
.
siblings() Method
In order to fetch all the siblings of a specified element, the siblings() method is used. This method traverses backwards as well as forward in the DOM tree to fetch all the siblings of an element.
Syntax
$(selector).siblings(filter)
Filter is an optional parameter that is used to specify an element to restrict the search for the siblings.
Example
This example demonstrates the working of the siblings() method.
HTML
In the above code, we have created a
element and nested a- element inside that div. Moreover, we have nested some
About the author
<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/B49B6FDCE1F74B3598C4C9BF564AEBEC-150×150.jpg" height="112" src="data:image/svg xml,” width=”112″>
Naima Aftab
I am a software engineering professional with a profound interest in writing. I am pursuing technical writing as my full-time career and sharing my knowledge through my words.
Show CommentsOther stories
Switch statement in Java | Explained
JQuery Sliding Methods Explained
- element is regarded as a direct parent of the element,
- , and . Observing their hierarchy, the
Output
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image5-37.png" data-lazy- height="380" src="data:image/svg xml,” width=”666″>
The successors of the
- element were found.
first() Method
The first() method fetches the first nested element inside a specified element.
Syntax
Example
Suppose you want to fetch the first element that is inside an
element. Follow the code below.
HTML
In the above HTML code, we have created an
element and nested two elements within that
element.
jQuery
Here we have used the first() method to find the first element of the
element, moreover, used css() method to highlight the fetched element.
Output
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image8-26.png" data-lazy- height="104" src="data:image/svg xml,” width=”856″>
The first element of the
element is highlighted in pink.
next() Method
In order to find the adjacent sibling of a specified element, the next() method is used.
Syntax
Filter is an optional parameter that is used to specify an element to restrict the search for the adjacent sibling.
Example
Here we have illustrated the working of the jQuery next() method.
HTML
Here we have created one
element and two
elements.
jQuery
In the above code, we are using the next() method to find the adjacent sibling of the
element.
Output
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image7-34.png" data-lazy- height="178" src="data:image/svg xml,” width=”621″>
The adjacent sibling of the
element was fetched successfully.
parent () Method
For the purpose of finding the direct parent of an element, the parent() method is used. It is a built-in jQuery function that only goes one level up the specified element and fetches the immediate parent of that element.
Syntax
$(selector).parent(filter)
Filter is an optional parameter which is used to restrict the search for parent element by specifying a selector expression.
Example
Suppose you want to fetch the direct parent of a element which is present in an
- element which further is a part of a
CSS
.main * {
display: block;
border: 2px solid gray;
color: gray;
padding: 5px;
margin: 15px;
}
We styled the class “main” using CSS. This class is assigned to body.
jQuery
Here we are using the find() method to find
- and elements in the
- .
Output
<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image4-51.png" data-lazy- height="315" src="data:image/svg xml,” width=”668″>
The direct child of
- has been fetched successfully.
each() Method
In order to specify a function to be executed for each of the specified elements, the each() method is used.
Syntax
$(selector).each(function(index,element))
Example
Suppose you want to change the text for each of the
element using the jQuery each() method.
HTML
In the above HTML code, we have created three
elements and one button.
jQuery
Here we are using the each() method to change text for each of the
elements. The “this” parameter refers to the current element, which in this case is the
element.
Output
The text changes everytime you click on the button.
<img alt="" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/03/echo/image6-4.gif" height="268" src="data:image/svg xml,” width=”311″>
The each() method is working properly.
find() Method
For the purpose of finding all the successors of an element, the find() method is used. All successors of an element include; children, grandchildren, great-grandchildren, and so forth.
Syntax
Filter is a required parameter which refers to either a selector, element, or jQuery object whose children are to be found.
Example
Suppose you want to fetch the child, grandchild and great-grandchild of an element. Here is how you do it.
HTML
In the above code, a
element is created and inside thatwe have nested a- element. Furthermore, we have nested
- element