jQuery Traversing - Descendants
You can use jQuery to explore inside the HTML structure and locate elements that are inside another element.
A descendant is someone's child, grandchild, great-grandchild, and so forth.
Traversing Down the DOM Tree
Here are two helpful jQuery techniques for navigating through the tree-like structure of a webpage:
jQuery children() Method
The children() function gives you all the immediate children of the element you've chosen.
This approach only goes one level deeper into the web page's structure.
This example finds all the elements that are directly inside each <div> element:
You have the choice to use an extra option to narrow down the search for children.
This example finds and displays all paragraphs (<p>) within a specific <div> element that has the class name "first." It only looks at the paragraphs directly inside that <div>.
jQuery find() Method
The find() function gives you all the elements inside the chosen element, including its deepest descendants.
In this example, we find and display all <span> elements inside a <div>.
This example retrieves all elements inside a <div> element:
<!DOCTYPE html>
<html>
<head>
<style>
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("div").children().css({
"color": "red",
"border": "2px solid red"
});
});
</script>
</head>
<body>
<div class="descendants" style="width:500px;">div (current element) <p>p (child) <span>span (grandchild)</span>
</p>
<p>p (child) <span>span (grandchild)</span>
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("div").children("p.first").css({
"color": "red",
"border": "2px solid red"
});
});
</script>
</head>
<body>
<div class="descendants" style="width:500px;">div (current element) <p class="first">p (child) <span>span (grandchild)</span>
</p>
<p class="second">p (child) <span>span (grandchild)</span>
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("div").find("span").css({
"color": "red",
"border": "2px solid red"
});
});
</script>
</head>
<body>
<div class="descendants" style="width:500px;">div (current element) <p>p (child) <span>span (grandchild)</span>
</p>
<p>p (child) <span>span (grandchild)</span>
</p>
</div>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<style>
.descendants * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 5px;
margin: 15px;
}
</style>
<script src="https://code.jquery.com/jquery-3.7.1.js" integrity="sha256-eKhayi8LEQwp4NKxN+CfCh+3qOVUtJn3QNZ0TciWLP4=" crossorigin="anonymous"></script>
<script>
$(document).ready(function() {
$("div").find("*").css({
"color": "red",
"border": "2px solid red"
});
});
</script>
</head>
<body>
<div class="descendants" style="width:500px;">div (current element) <p>p (child) <span>span (grandchild)</span>
</p>
<p>p (child) <span>span (grandchild)</span>
</p>
</div>
</body>
</html>