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

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Word token match

What You’ll Learn

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

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.

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 (*=).

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

⚡ Quick Reference

name value[name~="man"][name*="man"]
manMatchesMatches
mr manMatchesMatches
humanNoMatches
supermanNoMatches
womanNoMatches

📋 [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

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

How It Works

jQuery splits each name on whitespace. Only inputs with a token exactly equal to man are updated — substring matches inside one word do not count.

Example 2 — Test Word Matching with .is()

Compare several name values against the word selector programmatically.

jQuery
var names = [ "man", "mr man", "human", "superman", "bat man" ];

names.forEach( function( n ) {
  var $el = $( "<input>" ).attr( "name", n ).appendTo( "body" );
  console.log( n, "→", $el.is( "[name~='man']" ) ? "MATCH" : "no" );
  $el.remove();
});
Try It Yourself

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

How It Works

In production you would usually write $(".active"). The [class~='active'] form shows the same token logic the class selector uses under the hood.

Example 4 — Side-by-Side: ~= vs *= on class

See why the jQuery docs suggest ~= over *= for token attributes.

jQuery
$( "#tiles span" ).each( function() {
  var $t = $( this );
  console.log(
    $t.attr( "class" ),
    "| ~:", $t.is( "[class~='btn']" ),
    "| *:", $t.is( "[class*='btn']" )
  );
});
Try It Yourself

How It Works

*= treats btn as a substring — it falsely matches toolbar. ~= requires btn as its own space-separated word.

Example 5 — Scoped #panel [rel~='noopener']

Apply behavior only to links inside a panel whose rel token list includes noopener.

jQuery
$( "#panel a[rel~='noopener']" ).addClass( "external-safe" );

// rel="noopener noreferrer" → matches (noopener is a word)
// rel="external" outside #panel → ignored
Try It Yourself

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.

🚀 Common Use Cases

  • Class tokens[class~='selected'] when mirroring selector logic in JS.
  • rel attributesa[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.

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

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

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.

💡 Best Practices

✅ Do

  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about [attr~="value"]

Whole-word matching in token lists.

5
Core concepts
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.

Continue to [attr$=value]

Learn suffix matching for file extensions and naming conventions.

Attribute ends-with 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.

6 people found this page helpful