jQuery Traversing - Descendants
You may use jQuery to locate descendants of an element by traversing down the DOM tree.
A descendant is defined as a child, grandchild, great-grandchild, and so on.
Traversing Down the DOM Tree
Two helpful ways to traversing the DOM tree are jQuery:
children()
find()
Related Links
jQuery children() Method
All immediate children of the chosen element are returning with the children()
approach.
Only single level of the DOM tree is traversed by this technique.
Example:- The example below retrieves all elements which are direct children of
each div
element with class="div1".
div (current element)
p (child 1)
span (grandchild 1)
p (child 2)
span (grandchild 2)
div (child 3)
Output :-
p (child 1) span (grandchild 1)
p (child 2) span (grandchild 2)
Related Links
jQuery find() Method
The find()
method is used to find any child or descendant element anywhere from the choosen element.
The descendant elements of the chosen element are returned by the find()
function, all the way down to the final descendant.
Example :-
div (current element)
p (child 1)
span (grandchild 1)
p (child 2)
span (grandchild 2)
div (child 3)
Output :-
p (child 1) span (grandchild 1)
p (child 2) span (grandchild 2)