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
Fundamentals
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.
Concept
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).
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" );
Cheat Sheet
⚡ Quick Reference
Element text
:contains('John')
John Resig
Matches
Malcom John Sinclair
Matches (substring)
George Martin
No match
J. Ohn
No match (not “John”)
john (lowercase) with selector 'John'
No match (case-sensitive)
Compare
📋 :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
Hands-On
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"
product divs mentioning "sale" → on-sale class
Other product divs → unchanged
How It Works
Start with a fast CSS selector, then apply :contains() via .filter() on the smaller set.
Applications
🚀 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.
Important
📝 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)").
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
:contains()Universal
Bottom line: Scope before :contains(). Use [attr*="value"] for attributes, not visible text.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :contains()
Find elements by visible text content.
5
Core concepts
txt01
:contains()
Text match
API
Aa02
Case
Sensitive
Rule
sub03
Substring
Not word-only
Tip
#04
Scope
Prefix first
Perf
John05
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.