---
title: "Use of the jQuery:contains() selector"  
description: "Use of the jQuery:contains() selector"  
author: "Ethan Karla"  
published: 2021-07-28  
updated: 2023-06-14  
canonical: https://www.mindstick.com/forum/156661/use-of-the-jquery-contains-selector  
category: "jquery"  
tags: ["jquery"]  
reading_time: 2 minutes  

---

# Use of the jQuery:contains() selector

What is the use of the jQuery:contains() [selector](https://www.mindstick.com/interview/23419/action-selector-in-mvc)?

## Replies

### Reply by Aryan Kumar

The jQuery :contains() selector selects elements that contain the specified string. The text can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above). Note: The text is case sensitive.

Here is an example of how to use the :contains() selector:

Code snippet

```plaintext
// Select all elements that contain the text "Hello World"
$("*:contains('Hello World')");
```

This will select all elements that contain the text "Hello World". For example, if there is a div element with the text "Hello World" inside it, this selector will select it.

You can also use the :contains() selector to select elements that contain a part of a string. For example, the following selector will select all elements that contain the word "World":

Code snippet

```plaintext
$("*:contains('World')");
```

This will select all elements that contain the word "World", even if the word is not surrounded by spaces. For example, if there is a div element with the text "HelloWorld", this selector will select it.

The :contains() selector can be a very useful tool for selecting elements that contain specific text. It can be used to filter elements, to add or remove CSS styles, or to perform other tasks.

Here are some other examples of how to use the :contains() selector:

- To select all elements that contain the text "Hello World" and have a red background, you could use the following code:

Code snippet

```plaintext
$("*:contains('Hello World')")
  .css("background-color", "red");
```

- To remove all elements that contain the word "World" from a page, you could use the following code:

Code snippet

```plaintext
$("*:contains('World')").remove();
```

- To add a click event handler to all elements that contain the text "Hello World", you could use the following code:

Code snippet

```plaintext
$("*:contains('Hello World')").on("click", function() {
  // Do something when the element is clicked
});
```

The :contains() selector is a powerful tool that can be used to select elements based on the text they contain. It can be used to filter elements, to add or remove CSS styles, or to perform other tasks.


---

Original Source: https://www.mindstick.com/forum/156661/use-of-the-jquery-contains-selector

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
