---
title: "List of only the elements that contains"
description:
  "How to effectively search for a string in DOM elements.  This blog discusses
  jQuery technique."
canonical_url: "https://www.bigbinary.com/blog/list-of-only-the-elements"
markdown_url: "https://www.bigbinary.com/blog/list-of-only-the-elements.md"
---

# List of only the elements that contains

How to effectively search for a string in DOM elements. This blog discusses
jQuery technique.

- Author: Neeraj Singh
- Published: April 12, 2010
- Categories: jQuery

I was toying with
[simple list filter plugin](http://kilianvalkhof.com/2010/javascript/how-to-build-a-fast-simple-list-filter-with-jquery)
and ended up with this markup.

```html
<div id="lab">
  <ul id="list">
    <li><a href="">USA</a></li>
  <ul>
  <p>
    <a href=''>USA</a>
  </p>
</div>
```

I want to get all links that contains the word `USA`. Simple enough. jQuery
supports `contains` selector.

```javascript
$(":contains('USA')");
```

Above query results in following items.

```ruby
[html, body#body, div#lab, ul#list, li, a, ul, p, a]
```

That is because [contains](http://api.jquery.com/contains-selector) looks for
given string under all the descendants.

## has method to rescue

jQuery [has](http://api.jquery.com/has) method which returns the list of
elements which have a descendant which has the given string.

```javascript
b = $("*").has(":contains('USA')");
```

Above query results in following items.

```ruby
[html, body#body, div#lab, ul#list, li, ul, p]
```

## Final result

```javascript
a = $(":contains('USA')");
b = $("*").has(":contains('USA')");
c = a.not(b);
console.log(c);
```

Above query results in following items.

```ruby
 [a, a]
```

## Links

- [Human page](https://www.bigbinary.com/blog/list-of-only-the-elements)
