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

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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
CallWhat it does
See the examples below.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Confirm the attribute is a whitespace-separated list (class, rel, multi-word name, …).
Step 2Write $("[attr~='word']") — or with a tag: $("input[name~='man']").
Step 3Remember: the word must match a full token, not part of a longer token.

Return Value

Return ValueDescription
Value / undefinedGetters 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
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.

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.

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.

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: Attribute Starts With Selector

Match attributes whose value starts with a given string.

Attribute Starts With Selector tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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