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

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

What Is [name|="value"]?

The attribute contains prefix selector matches elements whose attribute is exactly value, or starts with value followed by a hyphen (-). Available since jQuery 1.0 (standard CSS).

Remember
$("[attribute|='value']")

$("a[hreflang|='en']")
  ✓ hreflang="en"
  ✓ hreflang="en-US"
  ✗ hreflang="english"   (no hyphen after en)
  ✗ hreflang="fr"

[attr|="en"]  ≠  [attr^="en"]   // ^= also matches "english"
UsePrefer
Language / hyphen tags$("[lang|='en']")
Exact value only$("[attr='en']")
Any string start$("[attr^='en']")

How to Use [name|="value"]

Name the attribute, set the prefix, then query for exact or hyphen-extended values.

  1. Choose the attribute (hreflang, lang, data-version, …).
  2. Write $("[attr|='prefix']") — optional tag: $("a[hreflang|='en']").
  3. Remember: longer matches need a hyphen after the prefix (en-US, not enUS).
  4. Do not use |= when you only want a raw string start — use ^= instead.
PieceRequired?Role
attributeYesAttribute name to test
valueYesExact match, or prefix before a hyphen
Quotes around valueRecommendedRequired for many values; identifiers may omit them

Pseudocode

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

for each element with attrName:
    v = element.getAttribute(attrName)
    if v == prefix:
        add element                    // exact
    else if v starts with (prefix + "-"):
        add element                    // hyphen prefix

return jQuery(matches)

Cheat Sheet

Keep these facts handy while matching hyphenated attribute values.

TopicDetail
Syntax$("[attr|='value']")
Matchesvalue or value-*
Classic use$("a[hreflang|='en']")
vs == is exact only
vs ^=^= is any start (no hyphen rule)
vs ~=~= is a whole word in a space list
SincejQuery 1.0 (standard CSS)

Live Preview

Run prefix / starts-with / equals selectors against sample hreflang links and see which values match.

Sample markup mirrors the official jQuery hreflang demo, plus an english trap value.

Result
$("a[hreflang|='en']")
matched: 2
values: en, en-US

/* exact or hyphen prefix */

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: a[hreflang|="en"]

List English language links — exact en and hyphenated en-US.

jQuery
var values = $("a[hreflang|='en']").map(function () {
  return $(this).attr("hreflang");
}).get().join(", ");

console.log(values);
// en, en-US
Try It Yourself

How It Works

  1. Limit to anchors. The a tag keeps the search on links only.
  2. Apply |=. Values must be en or start with en-.
  3. Collect attributes. .map() prints each matching hreflang.
  4. Official pattern. Same idea as the jQuery API demo that outlines English links.

Example 2 — |= vs ^=

See why english matches starts-with but not the hyphen prefix selector.

jQuery
console.log("|= :", $("a[hreflang|='en']").length);
console.log("^= :", $("a[hreflang^='en']").length);

// |= skips hreflang="english"
// ^= includes "english" because it starts with "en"
Try It Yourself

How It Works

  1. Pipe rule. |= needs an exact value or a hyphen after the prefix.
  2. Caret rule. ^= only checks the starting characters.
  3. Trap value. english starts with en but has no hyphen → ^= only.
  4. Pick carefully. Language tags → |=. Loose prefix → ^=.

Example 3 — Exact = vs Prefix |=

Exact equals skips en-US; the prefix selector keeps it.

jQuery
console.log("exact = :", $("a[hreflang='en']").length);
console.log("prefix |=:", $("a[hreflang|='en']").length);
Try It Yourself

How It Works

  1. Equals is strict. [hreflang='en'] needs the full string en.
  2. Prefix widens. |= also accepts en-US, en-GB, and similar tags.
  3. Compare lengths. The count shows how many regional variants you keep.
  4. When to choose. One exact code → =. Whole language family → |=.

Example 4 — Match lang on Elements

The same hyphen-prefix rule works on the HTML lang attribute.

jQuery
var tags = $("[lang|='en']").map(function () {
  return this.tagName + "[" + this.lang + "]";
}).get().join(", ");

console.log(tags);
Try It Yourself

How It Works

  1. Any element. Omitting a tag name searches every node with lang.
  2. Same rule. en and en-GB match; fr does not.
  3. Label results. Mapping tag + language makes the match set easy to read.
  4. Real use. Style or script only the English-language regions of a page.

Example 5 — Hyphen Prefixes Beyond Languages

Any hyphenated token works — here, a data-version family.

jQuery
var list = $("[data-version|='2']").map(function () {
  return $(this).attr("data-version");
}).get().join(", ");

console.log(list);
// 2, 2-beta  (not 20 or 2beta)
Try It Yourself

How It Works

  1. Prefix token. 2 matches exactly or as 2-….
  2. Hyphen required. 2beta and 20 fail the rule.
  3. List values. Mapping data-version shows which tokens passed.
  4. Design tip. Structure attribute values with hyphens if you plan to use |=.

Edge Cases & Pitfalls

Check these before relying on |= in real projects.

trap

|= ≠ ^=

[hreflang|="en"] skips english. [hreflang^="en"] includes it. Pick the operator that matches your intent.

hyphen

No hyphen, no extended match

enUS or 2beta do not match a prefix of en / 2. The suffix must follow a hyphen.

missing

Missing attribute never matches

An element without the attribute is skipped — even if nearby siblings match.

case

Matching is case-sensitive in HTML attributes

In HTML documents, attribute selector matching is generally ASCII case-sensitive for values. Prefer consistent lowercase language tags.

Browser Support

The attribute contains prefix 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("a[hreflang|='en']"). Designed for language-style hyphenated values.

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 language tags and other hyphen-prefixed tokens; switch to ^= when you need a plain string prefix.

Performance Notes

ApproachCostBest for
$("a[hreflang|='en']")Native-friendly CSSTagged links / scoped queries
$("[lang|='en']")Wider scanAny element with lang
$("[attr^='en']")SimilarLoose prefixes (not language rules)

Attribute selectors are standard CSS, so jQuery can often delegate to the browser engine. Narrow with a tag (a[…]) or a container ($("#nav").find("a[hreflang|='en']")) on large pages.

Key Takeaways

  • Role: match exact value or value-* hyphen prefixes.
  • Classic use: language tags like en / en-US.
  • Not ^=: english matches ^=, not |=.
  • Standard CSS: works in jQuery 1.0+ and native querySelectorAll.

One line: use [attr|="value"] for exact or hyphen-prefixed values — ideal for language codes.

Frequently Asked Questions

It selects elements whose named attribute equals value exactly, or whose 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 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" (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 anchors with matching hreflang — the pattern used in the official jQuery demo.

Next: [name*="value"]

Match attributes that contain a substring anywhere in the value.

*= 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