English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery parents() Method

jQuery Traversal Methods

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

The parents() method traverses up the DOM tree from the parent element to the root element (html).

parents() andclosest()The similarities between the methods are that they both traverse the DOM tree. The differences are as follows:

parents()

  • Start from the parent element

  • Navigate to the root element of the DOM tree, add each ancestor element to a temporary set; then, if a selector is provided, filter the set based on the selector

  • The returned jQuery object contains zero or more elements for each element in the original set (in the reverse document order)

closest()

  • Start from the current element

  • Move up the DOM tree until an object matching the provided selector is found

  • The returned jQuery object contains zero or one element for each element in the original set, in document order

Syntax:

$ (selector).parents(filter)

Example

Return all ancestor elements of the SPAN element:

body (Great Great Grandparent)
div (Great Grandparent)
div (Grandparent)

p (Direct Parent)                 span

Run Code

Parameter Value

ParametersDescription
filter(Optional) Specify a selector expression to match the element with
Note:To return multiple ancestors, separate each expression with a comma

jQuery Traversal Methods