jQuery Attribute Contains Word [name~="value"]

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is [name~="value"]?

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).

Remember
$("[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
UsePrefer
Whole word in a list$("[attr~='man']")
Substring anywhere$("[attr*='man']")
Class token$(".btn") or $("[class~='btn']")

How to Use [name~="value"]

Use it when the attribute holds space-separated tokens and you need one exact word.

  1. Confirm the attribute is a whitespace-separated list (class, rel, multi-word name, …).
  2. Write $("[attr~='word']") — or with a tag: $("input[name~='man']").
  3. Remember: the word must match a full token, not part of a longer token.
  4. For a plain substring anywhere, use *= instead.
PieceRequired?Role
attributeYesAttribute that holds space-separated words
valueYesOne whole word to match exactly
Quotes around valueRecommendedRequired for many values; identifiers may omit them

Pseudocode

Pseudocode
attrName = given attribute
word     = given value
matches  = empty list

for each element with attrName:
    tokens = split(attribute value, by whitespace)
    if word is exactly equal to any token:
        add element

return jQuery(matches)

Cheat Sheet

Keep these facts handy while matching space-separated words.

TopicDetail
Syntax$("[attr~='value']")
MatchesExact word in a space-separated list
Classic use$("input[name~='man']")
vs *=*= = substring anywhere
vs |=|= = exact or hyphen prefix
Class shortcut.token ≈ [class~='token']
SincejQuery 1.0 (standard CSS)

Live Preview

Compare word / substring / equals selectors against sample inputs and see which name values match.

Sample markup mirrors the official jQuery name~='man' demo, plus class tokens.

Result
$("input[name~='man']")
matched: 2
names: man, milk man

/* whole word only */

jQuery Examples

Short, copy-ready snippets. Open a browser with jQuery loaded, or use Try It Yourself for a live editor.

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
Try It Yourself

How It Works

  1. Limit to inputs. The input tag keeps the search on form controls.
  2. Split on spaces. Each name is treated as a list of words.
  3. Exact token. Only lists that include the word man match.
  4. Update values. .val() runs on that whole-word match set.

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
Try It Yourself

How It Works

  1. Word rule. ~= needs an exact space-delimited token.
  2. Substring rule. *= finds man inside longer tokens.
  3. Compare counts. Lengths show how much wider *= is.
  4. Pick carefully. Token lists → ~=. Loose fragments → *=.

Example 3 — Match a Class Token with [class~="…"]

Same whole-word idea as .active, written as an attribute selector.

jQuery
var labels = $("[class~='active']").map(function () {
  return this.textContent.trim();
}).get().join(", ");

console.log(labels);
console.log("same count as .active:", $(".active").length);
Try It Yourself

How It Works

  1. Class is a word list. class="btn active" splits into tokens.
  2. Exact token. active matches; inactive does not.
  3. Compare to .active. Counts should match for normal class tokens.
  4. When to use. Prefer .active for styling; use ~= on non-class attributes.

Example 4 — Match a Word in rel

Select links that include a specific token in a multi-value rel attribute.

jQuery
var hrefs = $("a[rel~='nofollow']").map(function () {
  return $(this).attr("href");
}).get().join(", ");

console.log(hrefs);
Try It Yourself

How It Works

  1. Multi-value rel. Values like nofollow sponsored are word lists.
  2. Token match. ~='nofollow' finds that exact word in the list.
  3. Collect hrefs. Mapping shows which links matched.
  4. Practical use. Filter SEO-related link relationships without substring accidents.

Example 5 — Single-Word Attributes Still Work

A value with no spaces is one token — it matches if that token equals the search word.

jQuery
console.log("role~=button:", $("[role~='button']").length);
console.log("role=button :", $("[role='button']").length);

// For a single token, ~= and = often return the same element
Try It Yourself

How It Works

  1. One token list. role="button" splits to ["button"].
  2. Exact equality. That single token equals button, so ~= matches.
  3. Same as = here. With no extra words, = and ~= agree.
  4. When they differ. Add more space-separated tokens and only ~= stays flexible.

Edge Cases & Pitfalls

Check these before relying on ~= in real projects.

trap

~= ≠ *=

[name~="man"] skips human. [name*="man"] includes it. Use whole-word matching when tokens matter.

spaces

Only whitespace delimits words

Hyphens and underscores do not split tokens. milk-man is one word, not milk + man.

class

Prefer .class for classes

[class~="btn"] works, but $(".btn") is clearer and the usual choice for class tokens.

case

Case-sensitive tokens

In HTML, [rel~="nofollow"] does not match rel="NoFollow". Keep casing consistent.

Browser Support

The attribute contains word selector [attr~="value"] is standard CSS and has worked in jQuery since 1.0. Modern browsers implement it in native querySelectorAll.

✓ CSS · jQuery 1.0+

jQuery [name~="value"]

Supported in all modern browsers. Native: document.querySelectorAll("input[name~='man']"). Exact word match in space-separated lists.

100% Standard CSS selector
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
~= combinator Universal

Bottom line: Safe everywhere jQuery runs. Use for token lists (class, rel, multi-word names); switch to *= only when you truly want a substring.

Performance Notes

ApproachCostBest for
$(".active")Fastest for classesClass tokens
$("input[name~='man']")Native-friendly CSSScoped form controls
$("[rel~='nofollow']")Wider scanAny element with that rel token

Attribute selectors are standard CSS, so jQuery can often use the browser engine. For classes, prefer .token. Narrow with a tag or container on large pages.

Key Takeaways

  • Role: match an exact word in a space-separated attribute list.
  • Classic use: $("input[name~='man']") from the official demo.
  • Not *=: human matches *=, not ~=.
  • Classes: prefer .token; use ~= for rel and similar lists.

One line: use [attr~="value"] when you need an exact space-delimited word — not a loose substring.

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".

Next: [name^="value"]

Match attributes whose value starts with a given string.

^= 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.

9 people found this page helpful