Question

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

$('body')

and

$('html')

respectively, but how do I select the document root?

Was it helpful?

Solution

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);

OTHER TIPS

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);

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top