Easy Tutorial
❮ Misc Iswindow Jq Sel Not ❯

jQuery parents() Method

jQuery Traversing Methods

Example

Return all ancestor elements of the <span>:

Result:


Definition and Usage

The parents() method returns all ancestor elements of the selected element.

Ancestors are the parent, grandparent, great-grandparent, and so on.

DOM Tree: This method traverses the ancestors of the DOM element from the parent upwards until it reaches the document root element (<html>).

Note: If the filter parameter is empty, the method will select all ancestors from the direct parent up to <body> and <html>. Therefore, passing a selector expression to narrow down the search results is very useful.

This method is similar to closest() in that both traverse up the DOM tree, but with the following differences:



Other Related Methods:


Syntax

Parameter Description
filter Optional. Specifies a selector expression to narrow down the search for ancestor elements. <br> <br> Note: To return multiple ancestors, separate each expression with a comma.

More Examples

Narrow Down the Search

Return Multiple Ancestors

Demonstrate Ancestors by Tag Name


jQuery Traversing Methods

❮ Misc Iswindow Jq Sel Not ❯