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

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("[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"
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 1Choose the attribute (hreflang, lang, data-version, …).
Step 2Write $("[attr|='prefix']") — optional tag: $("a[hreflang|='en']").
Step 3Remember: longer matches need a hyphen after the prefix (en-US, not enUS).

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

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.

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.

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.

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: Attribute Contains Selector

Match attributes that contain a substring anywhere in the value.

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