jQuery :contains() Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

:contains(text) (since jQuery 1.1.4) selects elements whose text content includes the given string. The text may sit on the element itself, in any descendant, or both. Matching is case-sensitive. Quotes around the text are optional.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("div:contains('John')")   // text includes "John"
$("div:contains(John)")     // same — quotes optional
$(":contains('John')")      // avoid — scans every element

// Case-sensitive: "john" ≠ "John"
// Prefer a tag or parent: $("#list li:contains('apple')")
CallWhat it does
:contains()Learn the jQuery :contains() selector: find elements by text content, case-sensitive substring rules, text in descendants, official John demo, pitfalls, browser support, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Start with a tag or scoped selector: $("div:contains('…')") or $("#box p:contains('…')").
Step 2Pass the search string — with or without quotes; casing must match.
Step 3Remember parents match when a child holds the text (aggregated text content).

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Official div:contains('John')

Find every div whose text includes John.

jQuery
var labels = [];
$("div:contains('John')").each(function () {
  labels.push($.trim($(this).text()));
});

$("#out").text(
  "Matched: " + labels.length + "\n" +
  labels.join(", ")
);
Try It Yourself

How It Works

  1. Query. $("div:contains('John')") tests each div’s text.
  2. Substring. Any occurrence of John counts — not a whole-word match.
  3. Skip. George Martin and J. Ohn do not contain John.

Example 2 — Case-Sensitive Search

Official rule: casing must match exactly.

jQuery
$("#out").text(
  "contains('John'): " + $("div:contains('John')").length + "\n" +
  "contains('john'): " + $("div:contains('john')").length
);
Try It Yourself

How It Works

  1. Exact case. John matches the text in the DOM.
  2. Lowercase. john is a different string — zero matches.
  3. Rule. Always copy the casing from your content.

Example 3 — Text Inside Descendants

Parents match when a nested element holds the search string.

jQuery
var n = $("#card:contains('Sale')").length;
var spanOnly = $("#card span:contains('Sale')").length;

$("#out").text(
  "#card matches: " + n + "\n" +
  "span matches: " + spanOnly
);
Try It Yourself

How It Works

  1. Nested text. Sale lives inside a span.
  2. Parent. #card:contains('Sale') still matches the card.
  3. Child. You can also target the span directly.

Frequently Asked Questions

:contains(text) selects every element whose text content includes the given string — as direct text, inside descendants, or both. $("div:contains('John')") matches a div showing "John Resig" or "Malcom John Sinclair" because "John" appears in that element’s aggregated text.
Yes. Official jQuery docs state the search is case-sensitive. :contains("john") does not match "John Resig". Match the exact casing you expect in the DOM.
Yes. The matching text can appear directly in the element, in any descendant, or a combination. A parent div matches when a nested span holds the search string.
:contains() searches element text content. [name*="value"] searches HTML attribute values such as href or title — not rendered text. Use the right tool for text vs attributes.
No. :contains() is a jQuery extension — not valid CSS. It works inside $() / Sizzle only. document.querySelectorAll(":contains('John')") throws a syntax error.
Prefer scoping: $("#list li:contains('apple')") or $("div.item").filter(":contains('sale')"). Bare $(":contains(text)") scans every element and can be slow on large pages.

Next: :not() Selector

Learn how to exclude elements from a selection with :not().

:not() Selector tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful