Traversing is a processing of selecting specific elements based on their relationship to other elements.
Here are some classifications and its methods
It's the process of finding the parent ancestors of an element.
we have four ansestor traversl methods
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Traversal Example</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<style>
div {
padding: 10px;
margin: 5px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="targetElement">
<p>This is the target element.</p>
<div>
<span>Child element</span>
</div>
</div>
<script>
// Using parent() to select the direct parent and apply styling
$("#targetElement").parent().css("background-color", "lightblue");
// Using parents() to select all ancestors and apply styling
$("#targetElement").parents().css("border", "2px dashed red");
// Using parentsUntil() to select ancestors until a certain element and apply styling
$("#targetElement span").parentsUntil("body").css("font-weight", "bold");
// Using closest() to select the closest ancestor matching a selector and apply styling
$("#targetElement span").closest("div").css("color", "green");
</script>
</body>
</html>
It's the process of finding the descendants of an element.
we have only two descendant traversl methods
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Child and Descendant Traversal Example</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<style>
div {
padding: 10px;
margin: 5px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div id="parentElement">
<p>This is the parent element.</p>
<div>
<span>Child element 1</span>
<span>Child element 2</span>
</div>
</div>
<script>
// Using children() to select direct children and apply styling
$("#parentElement").children().css("background-color", "lightblue");
// Using find() to select descendants and apply styling
$("#parentElement").find("span").css("font-weight", "bold");
</script>
</body>
</html>
It's the process of finding the descendants of an element.
we have only seven sibling traversl methods
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>jQuery Sibling Traversal Example</title>
<script src="https://code.jquery.com/jquery-3.6.4.min.js"></script>
<style>
div {
padding: 10px;
margin: 5px;
border: 1px solid #ccc;
}
.highlight {
background-color: lightyellow;
}
</style>
</head>
<body>
<div id="middleElement">
<p>First paragraph.</p>
</div>
<script>
// Using siblings() to select all siblings and apply styling
$("#middleElement").siblings().addClass("highlight");
// Using next() to select the immediately following sibling and apply styling
$("#middleElement p").next().css("color", "blue");
// Using nextAll() to select all following siblings and apply styling
$("#middleElement p").nextAll().css("font-style", "italic");
// Using nextUntil() to select siblings until a certain element and apply styling
$("#middleElement p").nextUntil("div").css("text-decoration", "underline");
// Using prev() to select the immediately preceding sibling and apply styling
$("#middleElement p").prev().css("color", "green");
// Using prevAll() to select all preceding siblings and apply styling
$("#middleElement p").prevAll().css("border", "2px dashed red");
// Using prevUntil() to select siblings until a certain element and apply styling
$("#middleElement p").prevUntil("body").css("margin", "15px");
</script>
</body>
</html>
The Concepts and codes you leart practice in Compilers till you are confident of doing on your own. A Various methods of examples, concepts, codes availble in our websites. Don't know where to start Down some code examples are given for this page topic use the code and compiler.