The attribute contains prefix selector matches elements whose attribute is exactlyvalue, 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"
Call
What it does
See the examples below.
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Choose the attribute (hreflang, lang, data-version, …).
Limit to anchors. The a tag keeps the search on links only.
Apply |=. Values must be en or start with en-.
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"
Equals is strict.[hreflang='en'] needs the full string en.
Prefix widens.|= also accepts en-US, en-GB, and similar tags.
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.