---
title: "has() method in jQuery"  
description: "has() method in jQuery"  
author: "Ethan Karla"  
published: 2021-07-29  
updated: 2023-06-14  
canonical: https://www.mindstick.com/forum/156670/has-method-in-jquery  
category: "jquery"  
tags: ["jquery"]  
reading_time: 2 minutes  

---

# has() method in jQuery

What is the has() [method in jQuery](https://www.mindstick.com/forum/156655/trim-method-in-jquery)? [Explain](https://www.mindstick.com/forum/157854/what-is-system-debugging-explain-some-system-debugging-tools-used-in-modern-computer-systems) it with the help of an example.\

## Replies

### Reply by Aryan Kumar

The has() [method](https://www.mindstick.com/forum/166/webservice-method) in jQuery is used to select all elements that have one or more elements inside of them that match the specified selector. The selector can be a string or a jQuery object.

The following code selects all <div> elements that have a <span> element inside of them:

Code snippet

```plaintext
$('div').has('span');
```

The following code selects all <p>, <h3>, and <div> elements that have a <span> element inside of them:

Code snippet

```plaintext
$('p, h3, div').has('span');
```

The following code selects a <span> element that has a hyperlink:

Code snippet

```plaintext
$('span').has('a');
```

The has() method can also be used to select elements that have multiple elements inside of them that match the specified selector. For example, the following code selects all <div> elements that have both a <span> element and an <a> element inside of them:

Code snippet

```plaintext
$('div').has('span, a');
```

The has() method is a powerful tool that can be used to select elements based on their descendants. It can be used to select elements that have specific content, elements that have a certain structure, or elements that are related to other elements.

Here are some additional examples of how the has() method can be used:

- To select all <div> elements that have a child <span> element with the text "Hello, world!", use the following code:

Code snippet

```plaintext
$('div').has('span:contains("Hello, world!")');
```

- To select all <div> elements that have a child <span> element with a class of "my-class", use the following code:

Code snippet

```plaintext
$('div').has('span.my-class');
```

- To select all <div> elements that have a child <span> element that is a descendant of a <ul> element, use the following code:

Code snippet

```plaintext
$('div').has('ul > span');
```

The has() method is a versatile tool that can be used to select elements in a variety of ways.


---

Original Source: https://www.mindstick.com/forum/156670/has-method-in-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
