The attribute contains word selector — written [name~="value"] — matches when a whole word in a space-separated attribute value exactly equals your search string. It is the right tool for class lists and other token attributes, available since jQuery 1.0.
01
Syntax
[attr~="x"]
02
Words
Space-delimited
03
Exact token
Whole word only
04
Official
name~=man
05
vs *=
Not substring
06
Since 1.0
CSS + jQuery
Fundamentals
Introduction
Some HTML attributes hold lists of tokens separated by spaces. The class attribute is the familiar example — class="btn primary active" contains three words. The contains word selector lets you target elements where one of those words exactly matches your search string.
The tilde equals syntax [attribute~="value"] splits the attribute on whitespace and compares each token to value. A match requires equality — not a substring inside a longer token. jQuery has supported this selector since 1.0, and it is standard CSS you can use in stylesheets too.
Concept
Understanding the Contains Word Selector
Imagine the attribute value as a row of tiles separated by spaces. The selector asks: “Is any single tile exactly equal to my search word?”
class="nav active open" + [class~="active"] → matches (word found).
class="inactive" + [class~="active"] → no match (active is inside a longer token).
name="mr man" + [name~="man"] → matches (man is its own word).
name="human" + [name~="man"] → no match (only one word: human).
💡
Beginner Tip
For everyday class styling, .active is shorter than [class~="active"]. Use ~= when the attribute is not class — or when you are learning how token matching differs from substring matching (*=).
Foundation
📝 Syntax
Official jQuery API form (since 1.0):
jQuery
jQuery( "[attribute~='value']" )
// shorthand:
$( "[attribute~='value']" )
// common on class tokens:
$( "[class~='active']" )
Parameters
attribute — attribute name (class, name, rel, etc.).
value — exact word to find among space-separated tokens.
Return value
A jQuery object with every element whose attribute word list includes an exact match.
Empty collection when no token equals the search string.
Official jQuery API example
jQuery
$( "input[name~='man']" ).val( "mr. man is in it!" );
// Matches name="man", name="mr man", name="bat man"
// Does NOT match name="human" or name="superman"
Cheat Sheet
⚡ Quick Reference
name value
[name~="man"]
[name*="man"]
man
Matches
Matches
mr man
Matches
Matches
human
No
Matches
superman
No
Matches
woman
No
Matches
Compare
📋 [attr~="word"] vs [attr*="word"]
Word selector requires a exact token; contains selector matches any substring — the docs recommend ~= for many class-like cases.
Word ~
[class~="btn"]
Token equals btn
Contains *
[class*="btn"]
btn anywhere — toolbar too
Class .
.btn
Shorthand for class
Best for
rel role
Non-class token attrs
Hands-On
Examples Gallery
Example 1 follows the official jQuery API demo. Examples 2–5 contrast ~= with *=, demonstrate class tokens, and show scoped selection. Use the Try-it links to run each snippet.
📚 Official jQuery Demo
Set value on inputs whose name attribute lists contain the word man.
Example 1 — Official Demo: input[name~='man']
Official jQuery demo — match inputs where man is a separate word in the name attribute.
jQuery
$( "input[name~='man']" ).val( "mr. man is in it!" );
// Matches: name="man", name="mr man", name="bat man"
// Skips: name="human", name="superman", name="woman"
man → MATCH
mr man → MATCH
human → no
superman → no
bat man → MATCH
How It Works
human and superman fail because man is not a standalone token — unlike the substring selector [name*="man"], which would match them.
📈 Practical Patterns
Class tokens, operator comparison, and scoped queries.
Example 3 — Highlight Elements with [class~='active']
The classic token use case — find elements that include active as a class word, not as part of inactive.
jQuery
$( "[class~='active']" ).addClass( "ring" );
// Matches class="tab active"
// Matches class="nav item active open"
// Does NOT match class="inactive" or class="activate"
Panel links with rel containing noopener token → class "external-safe"
Other panel links and outside links → unchanged
How It Works
rel often holds multiple space-separated link types. The word selector picks out one token without matching unrelated values that merely contain the same letters.
Applications
🚀 Common Use Cases
Class tokens — [class~='selected'] when mirroring selector logic in JS.
rel attributes — a[rel~='nofollow'] for SEO-related links.
role lists — [role~='button'] on elements with multiple roles.
Multi-value names — rare space-separated name attributes in legacy forms.
aria-* lists — token-style accessibility attributes in custom widgets.
Teaching — contrast with *= to explain substring vs word matching.
🧠 How jQuery Evaluates [attr~="value"]
1
Read attribute
Get the full string value of the named attribute from each element.
DOM
2
Split on spaces
Treat the value as a list of whitespace-delimited words (tokens).
Tokenize
3
Exact word match
If any token exactly equals the search string, the element matches.
Compare
4
~
Return collection
Matched elements become a jQuery object for chaining further methods.
Important
📝 Notes
Available since jQuery 1.0 — standard CSS attribute selector.
Designed for space-separated token lists (class, rel, etc.).
Does not match substrings inside a single token — use *= for that.
Matching is case-sensitive in HTML for attribute values.
Multiple spaces between tokens are normalized by the selector engine.
For single-value attributes without spaces, only an exact full-value match works if the value equals one word.
Compatibility
Browser Support
The attribute contains word selector is part of CSS2.1 and works in jQuery 1.0+. Evergreen browsers support it in native querySelectorAll. For classes, the .classname shortcut remains the most common choice.
✓ CSS2.1 · jQuery 1.0+
jQuery [attribute~="value"]
Supported in all modern browsers and IE7+ for attribute selectors. Native: document.querySelectorAll("[class~='active']").
100%Standard CSS selector
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
[attr~="value"]Universal
Bottom line: Use ~= for token lists; use *= only when you truly need substring matching. Prefer .className shorthand when selecting by class in jQuery.
Wrap Up
Conclusion
The attribute contains word selector [name~="value"] matches when a whitespace-separated token exactly equals your search string. jQuery’s official demo — $("input[name~='man']").val("mr. man is in it!") — shows the pattern on multi-word name values.
Reach for ~= on token attributes like class and rel. When you need substring flexibility, use [attr*="value"] instead — but expect broader matches.
Use [class~='token'] to mirror class token logic explicitly
Prefer ~= over *= for space-separated lists
Use .active shorthand when selecting by class only
Test with .is("[attr~='word']") when learning edge cases
Scope queries: $("#nav [class~='open']")
❌ Don’t
Expect human to match [name~="man"]
Use *= on class when you mean a whole class name
Assume hyphenated single tokens split further (primary-btn is one word)
Forget case sensitivity in HTML attribute matching
Replace every .class selector with [class~=] unnecessarily
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about [attr~="value"]
Whole-word matching in token lists.
5
Core concepts
~01
~= syntax
Word token
API
␣02
Spaces
Delimiters
Tokens
≠03
Not substring
Exact word
Rule
*04
vs *=
Stricter
Compare
.05
class
Main use
Pattern
❓ Frequently Asked Questions
It selects elements whose named attribute value is a list of space-separated words, where at least one word exactly equals the search string. For example, [class~="active"] matches class="btn active primary" because active is a whole word.
[attr*="man"] matches if "man" appears anywhere in the value — including inside "human" or "superman". [attr~="man"] matches only when "man" is its own whitespace-delimited word, such as class="hero man" or name="mr man".
CSS defines a word as a string delimited by whitespace. The attribute value is split on spaces; each token must exactly equal the search string to match.
Almost — both match elements with btn as a class token. The class selector .btn is shorter for styling. [class~="btn"] is useful when you need the same token logic on non-class attributes like rel or role.
Yes. The attribute contains word selector is standard CSS and has worked in jQuery since 1.0, with native querySelectorAll support in modern browsers.
No. "human" is a single word, not a list containing the separate word "man". That is the key difference from the substring selector [name*="man"], which would match "human".
Did you know?
jQuery’s docs for [attribute*="value"] explicitly note that [attr~="word"] is often more appropriate for space-separated lists — because substring matching can accidentally select toolbar when you wanted btn.