jQuery :lang() Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

:lang(language) (since jQuery 1.9) selects elements whose language matches the given code — equal to it, or starting with that code plus a hyphen (so :lang(en) matches en and en-US, but not english). Language usually comes from the lang attribute (and can inherit from ancestors).

Syntax

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

jQuery
$("div:lang(en)")          // lang="en", "en-US", "en-GB", …
$("div:lang(en-us)")       // en-us specifically (official demo)
$("p:lang(es)")            // Spanish paragraphs
$('[lang="en"]')           // exact attribute only — narrower!

// Inheritance: a child without lang= still matches
// if an ancestor sets the language
CallWhat it does
:lang()Learn the jQuery :lang() selector: match elements by language code, prefix matching for en vs en-US, inheritance from ancestors, vs [lang], examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Write $(":lang(en)") or $("div:lang(en-us)").
Step 2Use a short code (en) to catch regional variants (en-US, en-GB).
Step 3Use a full tag (en-us) when you need one region only.

Return Value

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

jQuery Examples

Example 1 — Official Demo: div:lang(en-us) / es-es

Mark USA and Spain blocks by language (jQuery API example, CSS-free).

jQuery
$("div:lang(en-us)").attr("data-region", "usa");
$("div:lang(es-es)").attr("data-region", "spain");

$("#out").text(
  "en-us: " + $("div:lang(en-us)").length +
  " · es-es: " + $("div:lang(es-es)").length
);
Try It Yourself

How It Works

  1. Select. div:lang(en-us) finds every div with that language.
  2. Inherit. Nested divs without lang still match the parent.
  3. Mark. data-region labels each matched block.

Example 2 — Prefix Match: :lang(en) vs :lang(en-us)

Short codes catch regional variants; full tags stay specific.

jQuery
console.log(":lang(en) →", $("div:lang(en)").length);
console.log(":lang(en-us) →", $("div:lang(en-us)").length);
console.log(":lang(en-gb) →", $("div:lang(en-gb)").length);
Try It Yourself

How It Works

  1. Prefix. :lang(en) matches en, en-us, and en-gb.
  2. Specific. :lang(en-us) matches only the US tag.
  3. Other region. en-gb is separate from en-us.

Example 3 — :lang(fr) vs [lang="fr"]

Attribute equality is exact and does not inherit; :lang() does both.

jQuery
console.log(":lang(fr) →", $("*:lang(fr)").length);
console.log('[lang="fr"] →', $('[lang="fr"]').length);
Try It Yourself

How It Works

  1. Pseudo. :lang(fr) uses resolved language (including inheritance).
  2. Attribute. [lang="fr"] needs that exact attribute on the element.
  3. Gap. A child without lang matches :lang but not [lang].

Frequently Asked Questions

:lang(language) selects elements whose language matches the supplied language code. $("div:lang(en)") matches lang="en" or lang="en-US" (code plus hyphen suffix), but not unrelated values like lang="english". Available since jQuery 1.9.
[lang="en"] matches only an exact lang attribute on that element. :lang(en) also matches lang="en-US", lang="en-GB", and elements that inherit language from an ancestor with a matching lang value.
Yes. :lang() is defined in the W3C CSS specification. It works in jQuery 1.9+ and in native querySelectorAll(":lang(en)") in modern browsers.
For HTML, language comes from the lang attribute on the element or ancestors, and possibly from meta elements or HTTP headers — as described in the official jQuery documentation.
Official jQuery demo runs $("div:lang(en-us)").addClass("usa") and $("div:lang(es-es)").addClass("spain") to style USA and Spain content blocks by language.
Yes. :lang(en) matches both lang="en" and lang="en-US" because en-US starts with en followed by a hyphen. Use :lang(en-us) when you need that regional tag only.

Next: :contains() Selector — Match by Text

Learn how to select elements by their text content with :contains().

:contains() Selector — Match by Text 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