jQuery parent() Method

The jQuery parent() method can be used to get the parent of the selected element. You can find the single or multiple parent element of the selected element. The parent can also be filtered by passing the parameter to the method.

jQuery parent() Method

Syntax

The syntax of this method is given below:-

Syntax
$(selector).parent(filter)

Description of the Parameters

The description of these parameters is given below.

Parameter Name Description
selector Specify the element to select its parent element. You can use the element tag name, class name, or id to select. It is a required field.
filter Specify the element to filter from the selected parent elements. You can specify single or multiple comma-separated elements to select. It is an optional field.

jQuery parent() Method to Get Parent Elements

If you want to get the parent of the selected element, you have to use the method as given below. The example contains the list element and button element.

Example

Test it Live

Output

  • Welcome to TutorialDeep.
  • This is a jQuery tutorial.
  • Learn from live examples.


When you click the button element given above, it selects the parent element of the specified em element. After selecting the element, it applies the color to the text of the element that contains em element.

Select parent Using Filter Single Element

If you want to select the specified parent element from the selected element, you have to use the method as given below. The example selects a single parent list element as specified.

Example

Test it Live

Output

  • Welcome to TutorialDeep.
  • This is a jQuery tutorial.
  • Learn from live examples.


Click the button given above to get the specified parent element for the selected em element. There are two list elements that contain the selected em element. However, only the last list element contains the class ‘last’.

Get Multiple parent Elements Using jQuery parent() Method

You can also find multiple parent elements of the selected element. To find the multiple parent elements, you have to specify the comma-separated tag name, class name, or id of the parent element.

Example

Test it Live

Output

  • Welcome to TutorialDeep.
  • This is a jQuery tutorial.
  • Learn from live examples.


When you click the button given above, it selects multiple specified parent elements from the selected element.

You may also like to read

I hope you like this tutorial on jQuery parent() method. If you have any queries regarding the tutorial, please comment below.

References