jQuery parent() method
Example
Return the direct parent element of the:
$(document).ready(function(){
$("span").parent().css({"color":"red","border":"2px solid red"});
});
Result:
body (great-great-grandparent)
div (great-grandparent)
- li (direct parent) span
Try it »
Definition and Usage
The parent() method returns the direct parent of the selected element.
**DOM Tree:** This method traverses only a single level up the DOM tree. To traverse all the way up to the document root element (returning the grandparent node or other ancestors), use parents() or parentsUntil() method.
**Tip:** To traverse a single level down the DOM tree, or all the way down to the last descendant (returning child nodes or other descendants), use children() or find() method.
Syntax
$(
selector
).parent(
filter
)
| Parameters | Description |
|---|---|
| filter | Optional. Specifies a selector expression to narrow down the search for parent elements. |
More examples
Return the direct parent element of the
How to return the direct parent element of the.
Narrow down the search
How to use the filter parameter to return each with class name "1" among the parent elements of the
the direct parent element of the with
How to return each
all direct parent elements of the with
Demonstrate ancestor elements of an element by tag name
Demonstrate ancestors of the element.
jQuery Traversal Methods