Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

jQuery parent() Method

❮ jQuery Traversing Methods

Example

Return the direct parent element of <span>:

$(document).ready(function(){
  $("span").parent().css({"color": "red", "border": "2px solid red"});
});

Result:

body (great-great-grandparent)
div (great-grandparent)
    ul (grandparent)
  • li (direct parent) span
Try it Yourself »

Definition and Usage

The parent() method returns the direct parent element of the selected element.

The DOM tree: This method only traverse a single level up the DOM tree. To traverse all the way up to the document's root element (to return grandparents or other ancestors), use the parents() or the parentsUntil() method.

Tip: To traverse a single level down the DOM tree, or all the way down to the last descendant (to return children or other descendants), use the children() or find() method.


Syntax

$(selector).parent(filter)

Parameter Description
filter Optional. Specifies a selector expression to narrow down the parent search

Try it Yourself - Examples

Return the direct parent of each <span> element
How to return the direct parent of each <span> element.

Narrow down the search
How to use the filter parameter to return the parent <li> element with class name "first" of each <span>.

Return the direct parent <div> element of each <p> element
How to return all the direct parent <div> element of each <p> element.

Show the ancestor elements of an element by tag names
A demonstration which shows who the ancestors of a <span> element actually are.


❮ jQuery Traversing Methods

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.