tomsv
tomsv

Reputation: 7277

select document root using jquery

I can select the body and html parts of the document using

$('body')

and

$('html')

respectively, but how do I select the document root?

Upvotes: 24

Views: 61832

Answers (3)

GetFree
GetFree

Reputation: 42514

The root of the DOM is always the html element.
You can get it either with $('html') or $(':root').

The following assertions should always be true:

$('html')[0] === $(':root')[0]
$(':root')[0] === document.documentElement

Upvotes: 4

adeneo
adeneo

Reputation: 318302

Not sure what you mean, but to select the document you do

$(document);

To get the contents of the document I'm guessing you need the documentElement, which is just the same as the <html> tag in most enviroments.

$(document.documentElement);

Upvotes: 39

Alok Jain
Alok Jain

Reputation: 3518

The Document interface inherits from Node, and represents the whole document, such as an HTML page. Although the Document node is conceptually the root of a document, it isn't physically the root - the root node is the first Element node in the Document, and is represented by its documentElement property.

You can select documentElement with following code:

var root = document.documentElement;

OR

$(document.documentElement);

Upvotes: 2

Related Questions