Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

jQuery parent() method

jQuery parent() method

jQuery Traversal Methods jQuery Traversal Methods

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)

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

  • .

    Return each

    the direct parent element of the with

    Element
    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 jQuery Traversal Methods