jQuery Attribute Contains Prefix [name|="value"]

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

What You’ll Learn

The attribute contains prefix selector — written [name|="value"] — matches elements whose attribute equals value or starts with value-. Built for language tags like en and en-US, it is part of standard CSS and jQuery since 1.0.

01

Syntax

[attr|="x"]

02

Exact

en matches

03

Prefix

en-US matches

04

Hyphen

Required rule

05

vs ^=

Stricter match

06

Since 1.0

CSS + jQuery

Introduction

HTML attributes carry metadata — language codes on links, version numbers on components, roles on custom elements. jQuery lets you filter the DOM with CSS attribute selectors inside $(), and the contains prefix form is one of the most specialized.

The pipe-equals syntax [attribute|="value"] means: “select this element if the attribute is exactly value, or if it begins with value followed by a hyphen.” CSS added this rule for language attributes where en, en-US, and en-GB share a common prefix. jQuery has supported it since version 1.0.

Understanding the Contains Prefix Selector

Think of the pipe (|=) as “equals or language-style extension.” The match succeeds in two cases:

  • Exact — attribute value is identical to the string (case-sensitive in HTML).
  • Hyphenated extension — value is the string, then -, then more characters.

It does not match values that merely contain the string elsewhere, start with similar letters without a hyphen boundary, or use underscores instead of hyphens.

💡
Beginner Tip

For [hreflang|="en"], the link with hreflang="english" is not selected — only en and codes like en-US qualify.

📝 Syntax

Official jQuery API form (since 1.0):

jQuery
jQuery( "[attribute|='value']" )
// shorthand:
$( "[attribute|='value']" )

// combine with element types:
$( "a[hreflang|='en']" )

Parameters

  • attribute — the HTML attribute name (e.g. hreflang, lang, data-version).
  • value — a valid identifier or quoted string to match as exact or hyphenated prefix.

Return value

  • A jQuery object containing every matched element in the search context.
  • Empty collection when no attributes satisfy the prefix rule.

Official jQuery API example

jQuery
$( "a[hreflang|='en']" ).css( "border", "3px dotted green" );

// Outlines links whose hreflang is "en" or "en-*"

⚡ Quick Reference

hreflang value[hreflang|="en"]
enMatches (exact)
en-USMatches (en + hyphen)
en-GBMatches
englishNo match
frNo match
enUS (no hyphen)No match

📋 [attr|="en"] vs [attr="en"] vs [attr^="en"]

Three attribute operators — exact, contains-prefix, and starts-with — behave differently on the same values.

Exact
[attr="en"]

Only en

Prefix |
[attr|="en"]

en or en-*

Starts ^
[attr^="en"]

Also english

Use case
hreflang

Prefer |= for locales

Examples Gallery

Example 1 follows the official jQuery API demo. Examples 2–5 explore matching rules, comparisons with other operators, and scoped selection. Use the Try-it links to run each snippet in the browser.

📚 Official jQuery Demo

Outline English-language links using hreflang.

Example 1 — Official Demo: a[hreflang|='en']

Find all links whose hreflang is English — exact en or regional variants like en-US.

jQuery
$( "a[hreflang|='en']" ).css( "border", "3px dotted green" );

// Matches:
//   <a hreflang="en">
//   <a hreflang="en-US">
// Does NOT match:
//   <a hreflang="fr"> or links without hreflang
Try It Yourself

How It Works

jQuery evaluates each anchor’s hreflang attribute against the prefix rule. Regional English codes share the en language subtag before the first hyphen, so en-US qualifies.

Example 2 — Which Values Match [lang|="en"]?

Loop sample values and log whether each matches — a clear way to learn the hyphen rule.

jQuery
var samples = [ "en", "en-US", "en-GB", "english", "fr", "enUS" ];

samples.forEach( function( code ) {
  var $el = $( "<span>" ).attr( "lang", code ).appendTo( "body" );
  var hit = $el.is( "[lang|='en']" );
  console.log( code, "→", hit ? "MATCH" : "no" );
  $el.remove();
});
Try It Yourself

How It Works

.is("[lang|='en']") tests a single element against the selector. Only exact en or en-… forms pass — not arbitrary strings starting with “en”.

📈 Practical Patterns

Compare operators, version attributes, and scoped queries.

Example 3 — [lang|="en"] vs [lang^="en"]

See why ^= is broader — it catches english, which locale-aware code usually wants to exclude.

jQuery
$( "#samples span" ).each( function() {
  var $s = $( this );
  console.log(
    $s.attr( "lang" ),
    "| pipe:", $s.is( "[lang|='en']" ),
    "| caret:", $s.is( "[lang^='en']" )
  );
});
Try It Yourself

How It Works

The starts-with operator (^=) checks characters only. The contains-prefix operator (|=) enforces a language-style boundary at a hyphen.

Example 4 — Version Prefix with [data-version|="1"]

Apply the same rule to custom data attributes — match major version 1 and sub-versions like 1-beta.

jQuery
$( "[data-version|='1']" ).addClass( "v1-line" );

// Matches data-version="1" and data-version="1-beta"
// Does NOT match data-version="10" or data-version="1.2" (dot ≠ hyphen rule)
Try It Yourself

How It Works

The hyphen rule is literal — 1.2 does not extend 1 because the next character is a dot, not a hyphen. Choose the operator that fits your naming scheme.

Example 5 — Scoped Selection Inside a Navigation Block

Limit English link highlighting to one menu — faster and clearer on large pages.

jQuery
$( "#main-nav a[hreflang|='en']" ).addClass( "locale-en" );

// Only English links inside #main-nav are styled
// Footer or sidebar links are ignored even if hreflang matches
Try It Yourself

How It Works

Combining an ID, tag name, and attribute selector narrows the search root. Browsers can optimize native selector engines when the chain starts with standard CSS.

🚀 Common Use Cases

  • Multilingual links — style or track a[hreflang|='en'] for English alternates.
  • Locale toggles — show UI hints on elements with lang|="en" in a translation preview.
  • API version blocks — bind handlers to [data-api|='v2'] widgets only.
  • Theme families[data-theme|='dark'] matches dark and dark-high-contrast.
  • QA highlighting — outline mis-tagged locale attributes during content audits.
  • Progressive enhancement — load English-only scripts when matching links exist.

🧠 How jQuery Evaluates [attr|="value"]

1

Read attribute

jQuery reads the named attribute string from each candidate element.

DOM
2

Exact check

If the value equals the search string exactly, the element matches.

Equal
3

Hyphen prefix

Otherwise, match if value is search + "-" + suffix.

Prefix
4

Return set

All passing elements become a jQuery collection ready for .css(), .addClass(), etc.

📝 Notes

  • Available since jQuery 1.0 — standard CSS attribute selector, not a jQuery-only extension.
  • Designed for language codes; also works on any attribute with hyphen-separated prefixes.
  • Matching is case-sensitive for attribute values in HTML documents.
  • Requires the attribute to be present — missing attributes never match.
  • Quote values containing special characters: [data-x|='en-US'].
  • Not the same as [attr*="en"] (contains) or [attr~="en"] (space-separated word).

Browser Support

The attribute contains prefix selector is part of CSS2.1 and works in jQuery 1.0+. Modern browsers accelerate it through native querySelectorAll — unlike jQuery-only pseudo-classes such as :animated.

CSS2.1 · jQuery 1.0+

jQuery [attribute|="value"]

Supported in all evergreen browsers, IE8+ with jQuery 1.x/2.x, and IE9+ with jQuery 3.x for standard attribute selectors. Native: document.querySelectorAll("a[hreflang|='en']").

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: Safe for production locale filtering. Prefer [attr|="en"] over [attr^="en"] when you need hyphen-boundary semantics for language or version codes.

Conclusion

The attribute contains prefix selector [name|="value"] matches exact attribute values and hyphenated extensions — the pattern CSS adopted for language tags. jQuery’s official demo, $("a[hreflang|='en']"), is the canonical introduction.

Reach for |= when you need locale-style prefixes; use ^= for simple starts-with checks and = for exact matches only. Scope with IDs or containers on large pages for clarity and speed.

💡 Best Practices

✅ Do

  • Use [hreflang|="en"] for BCP 47 language subtags
  • Combine with element types: a[hreflang|='en']
  • Scope to a container: $("#nav a[hreflang|='en']")
  • Quote values with hyphens or special characters
  • Test edge cases with .is("[attr|='value']")

❌ Don’t

  • Assume ^= and |= are interchangeable
  • Expect enUS to match [lang|="en"] without a hyphen
  • Use |= for semver with dots (1.2.3) — hyphens only
  • Forget case sensitivity in HTML attribute values
  • Match missing attributes — elements without the attribute are skipped

Key Takeaways

Knowledge Unlocked

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

Exact match or hyphenated prefix.

5
Core concepts
en 02

Exact

en OK

Match
- 03

en-US

Hyphen ext

Locale
^ 04

vs ^=

Stricter

Compare
🌐 05

hreflang

Main use

i18n

❓ Frequently Asked Questions

It selects elements whose named attribute equals value exactly, OR whose attribute value begins with value followed immediately by a hyphen (-). For example, [hreflang|="en"] matches hreflang="en" and hreflang="en-US", but not hreflang="english".
The CSS specification introduced it primarily for language attributes — BCP 47 language tags like en, en-US, and en-GB share a common language prefix separated by hyphens.
[attr^="en"] matches any value starting with the letters en — including "english" or "enable". [attr|="en"] is stricter: only "en" or "en-something" (with a hyphen after en) match.
Yes. Attribute selectors including the contains-prefix form have worked in jQuery since 1.0. Modern browsers also support it in native querySelectorAll.
Yes for extended matches. The prefix alone matches exactly. A longer match requires value + hyphen + suffix. "enUS" without a hyphen does not match [hreflang|="en"].
Yes. $("a[hreflang|="en"]") limits results to anchor elements with matching hreflang values — the pattern used in the official jQuery demo.
Did you know?

CSS defines four other attribute operators jQuery supports: = (exact), ^= (starts with), $= (ends with), and *= (contains substring). The pipe form |= is the only one that requires a hyphen after the prefix.

Continue to [attr*=value]

Learn the substring attribute selector — the most flexible value match.

Attribute contains 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