The attribute contains word selector matches elements whose attribute is a space-separated list, and at least one word exactly equals the search string. Available since jQuery 1.0 (standard CSS).
Syntax
Start with these common calls. Keep them short and readable.
jQuery
$("[attribute~='value']")
$("input[name~='man']")
✓ name="man"
✓ name="milk man"
✗ name="human" (one word — not "man")
✗ name="woman"
~= ≠ *= // *= also matches "human"
[class~='btn'] ≈ .btn // same token idea for class
Call
What it does
See the examples below.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Confirm the attribute is a whitespace-separated list (class, rel, multi-word name, …).
Step 2
Write $("[attr~='word']") — or with a tag: $("input[name~='man']").
Step 3
Remember: the word must match a full token, not part of a longer token.
Return Value
Return Value
Description
Value / undefined
Getters usually return a value from the first matched element, or undefined when nothing applies.
jQuery Examples
Example 1 — Official Demo: input[name~="man"]
Find inputs whose name contains the whole word man, then set a new value.
jQuery
$("input[name~='man']").val("mr. man is in it!");
var names = $("input[name~='man']").map(function () {
return this.name;
}).get().join(", ");
console.log(names);
// man, milk man
man, milk man
(human / woman skipped; matched values updated)
How It Works
Limit to inputs. The input tag keeps the search on form controls.
Split on spaces. Each name is treated as a list of words.
Exact token. Only lists that include the word man match.
Example 2 — ~= vs *=
See why human matches substring search but not whole-word search.
jQuery
console.log("~= :", $("input[name~='man']").length);
console.log("*= :", $("input[name*='man']").length);
// ~= skips "human" and "woman"
// *= includes them because "man" appears inside
Save
same count as .active: 1
(inactive is skipped)
How It Works
Class is a word list.class="btn active" splits into tokens.
Exact token.active matches; inactive does not.
Compare to .active. Counts should match for normal class tokens.
Frequently Asked Questions
It selects elements whose named attribute is a space-separated list of 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 — including inside "human". [attr~="man"] matches only when "man" is its own whitespace-delimited word, such as 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 [name*="man"], which would match "human".