Updated March 29, 2023
Definition on jQuery parent find
jQuery parent find a method of the specified element is returned by the parent() function, only a single level of the DOM tree is traversed by this approach.Parent() is similar to.parents(), except that.parent() only goes one step up the DOM tree. Optionally, the method accepts a selection expression of the same type as the $() function. If a selector is provided, the elements will be filtered based on whether they match or not. The parent() method returns the specified elements of the direct parent element. This approach only goes up one level in the DOM tree.
What is jQuery parent find?
Below is the syntax of the jQuery parent find method is as follows.
Syntax:
$(selector).parent (filter)
How does jQuery parent find works?
jQuery parent find examples
The below example shows, jQuery parent; find examples are as follows.
1) JQuery parent find an example to return each span direct parent element –
In the below example, we have defined border color as light grey and black. In ancestors, we need to define all the values.
Code:
<!DOCTYPE html>
<style>
.ancestors * {
display: block;
border: 1px solid lightgrey;
color: black;
padding: 4px;
margin: 10px;
</style>
<script>
$(document).ready(function(){
$("span").parent().css({"color": "Black", "border": "1px solid Blue"});
</script>
</head>
<div class="ancestors">
<div style="width:500px;"> (jQuery-Parent-method)
<ul> (JQuery)
<li> (Parent)
<span>Method of JQuery</span>
</body>
</html>
2) Using jQuery parent find a method, see the element of the paragraph using class –
The below example shows that a jQuery parent find a method to find the element of the paragraph using class as selected.
In the below example, we have defined the background color as red, and also we have defined the working color as black.
Code:
<!DOCTYPE html>
</head>
<div><p>Welcome to jQuery</p></div>
<div class="selected"><p>Example of jQuery parent find</p></div>
<script>$("p").parent(".selected").css ("background", "Red");</script>
</body>
</html>
3) JQuery parent find a method to return all ancestor elements –
The below example shows the jQuery parent find a method to return all ancestor elements are as follows.
Code:
<!DOCTYPE html>
<style>
.ancestors * {
display: block;
border: 2px solid lightgrey;
color: lightgrey;
padding: 4px;
margin: 10px;
</style>
<script>
$(document).ready (function (){
$("span").parent().css ({"color": "Black", "border": "1px solid Blue"});
</script>
</head>
<body class="ancestors"> (JQuery)
<div style="width:500px;"> (Parent)
<ul> (Find)
<li> (Method)
<span>JQuery</span>
</body>
</html>
Conclusion
In jQuery, the parent() method is used to locate the parent element of the currently selected element. In jQuery, the parent() method returns the selected element after traversing a one-level up. The parent() method returns the specified elements of the direct parent element.
Recommended Articles
This is a guide to jQuery parent find. Here we discuss the definition, syntax, What is jQuery parent find, how to work, and Examples with code. You may also have a look at the following articles to learn more –