jQuery :contains() Selector

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Text content

What You’ll Learn

The :contains() selector matches elements whose text content includes a given string — direct text, descendant text, or both. Available since jQuery 1.1.4; a jQuery extension (not CSS). Case-sensitive substring matching.

01

Syntax

:contains(text)

02

Text

Not attributes

03

Official

div:contains('John')

04

Case

Sensitive

05

Scope

li:contains()

06

Extension

jQuery only

Introduction

Most selectors target tags, classes, IDs, or attributes. Sometimes you need to find elements by what they say — a product name in a list, an error word in a paragraph, or a user’s name in a card. jQuery’s :contains() pseudo-class fills that gap.

Added in jQuery 1.1.4, :contains(text) scans the aggregated text of each candidate element. Official documentation notes the match is case-sensitive and the search string can appear in the element itself, in any descendant, or both. It is not standard CSS — you cannot use it in native querySelectorAll.

Understanding the :contains() Selector

Think of :contains() as a substring search over visible text content:

  • div:contains('John') → matches “John Resig” and “Malcom John Sinclair”.
  • Text inside nested <span> children counts toward the parent’s match.
  • :contains('john') does not match “John” — case must match.
  • It is a substring match — :contains('Ann') matches “Banner”.
💡
Beginner Tip

Quote the search text when it has spaces or special characters: :contains('John Resig'). Bare words work for simple tokens: :contains(John).

📝 Syntax

Official jQuery API form (since 1.1.4):

jQuery
jQuery( ":contains(text)" )



// common patterns:

$( "div:contains('John')" )

$( "#list li:contains('apple')" )

$( "p.alert:contains('error')" )

$( "div.item" ).filter( ":contains('sale')" )

Parameters

  • text — a string to look for (case-sensitive). May be quoted or, for simple words, unquoted.

Return value

  • A jQuery object containing every element whose text content includes the search string.
  • Empty collection when no text matches.

Official jQuery API example

jQuery
// Finds all divs containing "John" and underlines them

$( "div:contains('John')" ).css( "text-decoration", "underline" );

⚡ Quick Reference

Element text:contains('John')
John ResigMatches
Malcom John SinclairMatches (substring)
George MartinNo match
J. OhnNo match (not “John”)
john (lowercase) with selector 'John'No match (case-sensitive)

📋 :contains() vs Attributes and Methods

Text content, attribute substrings, and programmatic filtering.

:contains()
div:contains('Hi')

Visible text nodes

[attr*=]
[title*="Hi"]

Attribute values

.filter()
.filter(":contains(x)")

After CSS scope

.text()
if (el.text().indexOf…)

Manual check

Examples Gallery

Example 1 follows the official jQuery div:contains('John') demo. Examples 2–5 cover case sensitivity, scoped list search, alert paragraphs, and .filter(":contains()"). Use the Try-it links to run each snippet.

📚 Official jQuery Demo

Underline every div whose text includes “John”.

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

Official jQuery demo — underline divs containing the substring John.

jQuery
$( "div:contains('John')" ).css( "text-decoration", "underline" );



// Matches "John Resig" and "Malcom John Sinclair"

// Does NOT match "George Martin" or "J. Ohn"
Try It Yourself

How It Works

jQuery aggregates text from the element and its descendants, then tests for a case-sensitive substring match.

Example 2 — Case Sensitivity: 'John' vs 'john'

Show why casing must match exactly.

jQuery
console.log( "Upper:", $( "p:contains('John')" ).length );

console.log( "Lower:", $( "p:contains('john')" ).length );



// Same visible word, different selector casing → different counts
Try It Yourself

How It Works

Unlike many case-insensitive search UIs, jQuery’s :contains() compares strings with exact casing.

📈 Practical Patterns

Scoped searches, UI messages, and performance-friendly filtering.

Example 3 — Scoped List: $("#fruits li:contains('ap')")

Find list items whose label includes ap — matches apple and grape.

jQuery
$( "#fruits li:contains('ap')" ).css( "font-weight", "bold" );



// Substring match: "apple" and "grape" both contain "ap"
Try It Yourself

How It Works

Prefix with #fruits so only that list is scanned — faster and safer than bare $(":contains('ap')").

Example 4 — Alert Paragraphs: p:contains('error')

Highlight paragraphs that mention errors in their visible text.

jQuery
$( "p:contains('error')" ).addClass( "text-danger" );



// Matches "An error occurred" — not title="error" on another tag
Try It Yourself

How It Works

:contains() reads rendered text, not attribute values. Combine with a tag or class for precision.

Example 5 — Filter After Scope: .filter(":contains('sale')")

Narrow an existing collection — recommended on large pages.

jQuery
$( "div.product" )

  .filter( ":contains('sale')" )

  .addClass( "on-sale" );



// CSS class first, :contains second — better performance
Try It Yourself

How It Works

Start with a fast CSS selector, then apply :contains() via .filter() on the smaller set.

🚀 Common Use Cases

  • Name search — highlight cards containing a user’s name.
  • Menu filter — bold list items matching a quick search string.
  • Error scan — style paragraphs containing “error” or “warning”.
  • Table lookup — find rows where visible cell text includes a SKU fragment.
  • Demo scripts — official tutorials underline matching divs instantly.
  • Scoped filter.product.filter(":contains('new')") on catalog pages.

🧠 How jQuery Evaluates :contains()

1

Find candidates

Apply any tag, ID, or class prefix first — e.g. div:contains('John').

Scope
2

Aggregate text

Collect text from the element and all its descendants into one string.

Content
3

Substring test

Case-sensitive search for the literal text argument.

Match
4

Return collection

Matching elements become a jQuery object for chaining.

📝 Notes

  • Available since jQuery 1.1.4 — jQuery extension, not standard CSS.
  • Case-sensitive — match exact casing in the selector string.
  • Substring match — not whole-word only; Ann matches Banner.
  • Text in descendants counts toward the parent element’s match.
  • Quotes optional for simple words; use quotes for spaces and special characters.
  • Prefer scoped selectors or .filter(":contains()") over bare $(":contains(text)").

Browser Support

The :contains() selector is a jQuery extension — it works wherever jQuery 1.1.4+ runs. It is not supported in native querySelectorAll. Behavior is consistent across browsers because Sizzle evaluates text content the same way in each.

jQuery 1.1.4+ · Extension

jQuery :contains() Selector

Works in all jQuery-supported browsers. Not native CSS. Case-sensitive text substring matching via Sizzle.

100% jQuery engine
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:contains() Universal

Bottom line: Scope before :contains(). Use [attr*="value"] for attributes, not visible text.

Conclusion

The :contains() selector finds elements by visible text — a substring search that includes descendant text and respects case. The official div:contains('John') demo underlines every matching div in one line.

Scope your queries, distinguish text from attribute selectors, and use .filter(":contains()") after a CSS selector when pages grow large. For moving through matched nodes, continue to traversing methods next.

💡 Best Practices

✅ Do

  • Scope: #list li:contains('ap')
  • Match case exactly as in the DOM
  • Use .filter(":contains()") after a class selector
  • Quote strings with spaces: :contains('John Resig')
  • Combine with tags: p:contains('error')

❌ Don’t

  • Use bare $(":contains(text)") on huge documents
  • Expect word-boundary matching only
  • Confuse with [title*="text"] attribute search
  • Use in native querySelectorAll
  • Assume case-insensitive matching

Key Takeaways

Knowledge Unlocked

Five things to remember about :contains()

Find elements by visible text content.

5
Core concepts
Aa 02

Case

Sensitive

Rule
sub 03

Substring

Not word-only

Tip
# 04

Scope

Prefix first

Perf
John 05

Official

div demo

Demo

❓ 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 somewhere 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 visible element text content. [name*="value"] searches HTML attribute values such as href, title, or data-* — not rendered text nodes. Use the right tool for text vs attributes.
No. :contains() is a jQuery extension selector — not valid CSS. It works inside $() and jQuery's Sizzle engine only. Native document.querySelectorAll(":contains('John')") throws a syntax error.
Prefer scoping: $("#list li:contains('apple')") or $("div.item").filter(":contains('sale')") after a CSS selector. Bare $(":contains(text)") scans every element and can be slow on large pages.
Did you know?

Because :contains() matches substrings, searching for ap highlights both “apple” and “grape”. For whole-word matching, use .filter() with a custom function and a regex instead of relying on :contains() alone.

Continue to :not() Selector

After text selectors, learn how to exclude elements with the :not() negation filter.

:not() selector tutorial →

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.

6 people found this page helpful