jQuery :root Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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

Syntax

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

jQuery
$(":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
CallWhat it does
Selector string:root — no arguments
ContextUsually omitted; root is the document root
Return valuejQuery collection with exactly one element

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

Parameters

ParameterDescription
Step 1Call $(":root") — you always get one element.
Step 2In HTML pages, treat it as the <html> element.
Step 3Read properties with [0].nodeName, .attr(), or .prop().

Return Value

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

jQuery Examples

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.

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.

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.

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 — URL Hash Fragment

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

:target Selector — URL Hash Fragment 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