jQuery :root Selector

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

What Is :root?

:root selects the single element that is the root of the document. Official jQuery docs note that in HTML this is always the <html> element — the same node as document.documentElement.

Remember
$(":root")                    // document root (1 element)
$(":root")[0].nodeName        // "HTML" in HTML pages

$("html")                     // same node in HTML
document.documentElement      // same DOM node (no jQuery)

// Always exactly one match
NeedUse
Document root$(":root")
HTML tag by name$("html")
Native DOM rootdocument.documentElement
Everything under root$(":root *") or $(":root").find("*")
URL fragment target$(":target")

How to Use :root

Select the root once, then read its tag name, set attributes, or start a traversal from the top of the tree.

  1. Call $(":root") — you always get one element.
  2. In HTML pages, treat it as the <html> element.
  3. Read properties with [0].nodeName, .attr(), or .prop().
  4. Use it as a starting point for .find() when you want searches from the true document top.
  5. Prefer $("html") only when you specifically mean the HTML tag name.
PieceRequired?Role
Selector stringYes:root — no arguments
ContextOptionalUsually omitted; root is the document root
Return value—jQuery collection with exactly one element

Pseudocode

Pseudocode
root = document.documentElement   // top of the tree

return jQuery(root)

// In HTML:
//   root.tagName === "HTML"
//   $(":root")[0] === $("html")[0]

Cheat Sheet

Keep these facts handy while working with the document root.

TopicDetail
Syntax$(":root")
SincejQuery 1.9 · standard CSS
HTML rootAlways <html>
Match countExactly 1
vs $("html")Same node in HTML documents
vs documentElementSame DOM node without jQuery
Native CSSWorks in querySelector(":root")
Not body$("body") is a child of root

Live Preview

Compare :root with other ways to reach the top of this page’s document.

Runs against the live tutorial page document — same idea as the official nodeName demo.

Result
$(":root")
nodeName: HTML
length: 1
same as html? true

/* document root = <html> in HTML */

jQuery Examples

Short, copy-ready snippets. Open DevTools Console with jQuery loaded, or use Try It Yourself for a live editor.

Example 1 — Official Demo: Root nodeName

Display the tag name of the document root (jQuery API example).

jQuery
$("#log").text($(":root")[0].nodeName);
Try It Yourself

How It Works

  1. Select. $(":root") returns a one-element collection.
  2. Unwrap. [0] is the raw DOM node.
  3. Read. nodeName is "HTML" in HTML documents.
  4. Show. Writing that string into #log proves which node matched.

Example 2 — :root vs $("html")

In HTML pages both selectors point at the same element.

jQuery
var same = $(":root")[0] === $("html")[0];
console.log("same node?", same);
console.log("root length:", $(":root").length);
console.log("html length:", $("html").length);
Try It Yourself

How It Works

  1. Root. :root means “document root,” not a tag name.
  2. Tag. $("html") means “elements named html.”
  3. Compare. [0] === [0] checks they are the identical DOM node.
  4. Count. Both collections have length 1 in a normal HTML page.

Example 3 — :root vs document.documentElement

The native DOM property is the same node jQuery wraps.

jQuery
var root = $(":root")[0];
var native = document.documentElement;
var qsa = document.querySelector(":root");

console.log(root === native);
console.log(root === qsa);
console.log(native.nodeName);
Try It Yourself

How It Works

  1. jQuery. $(":root")[0] is the wrapped root node.
  2. DOM. document.documentElement is the same reference.
  3. CSS engine. querySelector(":root") also returns that node.
  4. Takeaway. :root is standard CSS — native APIs support it.

Example 4 — Read / Set Attributes on the Root

Use the root element to inspect or update document-level attributes like lang.

jQuery
var before = $(":root").attr("lang") || "(none)";
$(":root").attr("lang", "en");
var after = $(":root").attr("lang");

$("#out").text("before: " + before + "\\nafter: " + after);
Try It Yourself

How It Works

  1. Select root. Attributes on <html> apply to the whole document.
  2. Read. .attr("lang") returns the current language code if present.
  3. Write. Setting lang updates the root element in place.
  4. Verify. Reading again confirms the change.

Example 5 — Search Descendants from :root

Start at the document top, then find nested elements with .find().

jQuery
var n = $(":root").find("p").length;
$("#out").text("Paragraphs under :root: " + n);
Try It Yourself

How It Works

  1. Anchor. $(":root") is the top of the tree.
  2. Descend. .find("p") searches all descendants.
  3. Count. .length is how many paragraphs exist under the root.
  4. Note. On a full page this includes every <p> — scope further when you need a region only.

Edge Cases & Pitfalls

Check these before treating :root like a normal element selector.

not body

:root is not body

$("body") is a child of the root. Styles or attributes on root affect the whole document; body is the content region.

version

Needs jQuery 1.9+

Older jQuery builds lack :root. Use $("html") or document.documentElement as a fallback.

xml

Root tag may not be html

In XML/SVG documents the root element can have another name. :root still finds it; $("html") may not.

iframe

Each document has its own root

Inside an iframe, $(":root") is that frame’s document root — not the parent page’s <html>.

Browser Support

The :root pseudo-class is standard CSS and has been in jQuery since 1.9. It works in native querySelector / querySelectorAll in all modern browsers.

✓ CSS · jQuery 1.9+

jQuery :root Selector

Supported in jQuery 1.9+ and every modern browser. Native: document.querySelector(":root") === document.documentElement in HTML.

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
:root Universal

Bottom line: Safe on current jQuery builds. In HTML it always selects <html>; use $("html") if you must support jQuery before 1.9.

Performance Notes

ApproachCostBest for
document.documentElementFastestPlain DOM access
$(":root") / $("html")CheapjQuery chaining from the root
document.querySelector(":root")CheapNative CSS without jQuery

Because :root is standard CSS, jQuery can use the browser’s native selector engine. Cost is negligible — you are resolving a single, well-known node.

Key Takeaways

  • Role: select the single document root element.
  • HTML: root is always <html> — same as $("html").
  • Standard: works in native querySelector (unlike many jQuery form extensions).
  • Version: jQuery 1.9+; next topic is :target.

One line: $(":root") is the document’s top element — in HTML that means <html>, always exactly one match.

Frequently Asked Questions

:root selects the single element that is the root of the document. In HTML, official jQuery docs state that root is always the html element. Available since jQuery 1.9.
In HTML documents they target the same node — the documentElement. $('html') uses the tag name; $(':root') uses the document-root pseudo-class. In XML or SVG the root tag name may differ, but :root still finds the top-level element.
Always one — the single root node of the document tree. The official demo reads $(':root')[0].nodeName and displays it.
They target the same concept — the document root. CSS :root is standard and often used for custom properties on html. jQuery :root (since 1.9) selects that same root node in the DOM.
Yes. Unlike many jQuery form pseudo-classes, :root is standard CSS. document.querySelector(':root') returns the root element in modern browsers — equivalent to document.documentElement in HTML.
Use :root when you want document-root semantics (works even if the root tag is not html). Use $('html') when you specifically mean the HTML html element. For typical web pages they are interchangeable.

Next: :target Selector

Learn how to select the element that matches the current URL fragment identifier.

:target 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