jQuery :parent Selector

The jQuery :parent selector selects all the elements containing at least one element or text content. If you want to find the element that contains no other element or text content inside, you can use this selector.

Syntax of jQuery :parent() Selector

the syntax of the jQuery :parent selector is given below.

Syntax
$(“:parent”)

There is no parameter to specify for the selector. If you want to select only the specified content with the tag name. You can use the tag name, class name or id of the element before the selector.

Let’s make it more clear with the live examples given below.

jQuery :parent() Selector Select Parent Table Cells

if you want to highlight the table cells that contain at least one element or text inside. You can use the below example which applies the background color to only the selected elements. The example uses the click event of jQuery. However, you can use the example without using the click event.

Example

Test it Live

Output

Sr No Name
1 Bluetooth
2 Wi-Fi
3
4 Lan Cable
5

when you click the button given in the example above. it applies the background color ‘orange’ to the elements that contain another element, including the text content inside.

There are five rows with two cells contains nor text content. However, the third cell contains at least one element which is the HTML span. That’s why the selector also selects the third cell of the table to highlight.

Select All the Parent Elements in a List

In addition to the above example of the table, you can also use it with other elements like a list. It highlights the list items that contain at least one element or list content inside.

Example

Test it Live

Output

  • This is Tutorialdeep!
  • Learn Web Development with live examples.

There are five list items with two list items contains no text content. However, the last list item contains at least one element inside. The jQuery :parent selector select all the list items except the third one.

The selector selects all the elements including the last list item because it contains at least an HTML em tag element inside. It applies the background ‘orange’ color to the selected list items.