Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select document root using jquery

Tags:

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

$('body') 

and

$('html') 

respectively, but how do I select the document root?

like image 608
tomsv Avatar asked Jul 26 '12 09:07

tomsv


People also ask

How do you select an entire document in jQuery?

The * selector selects all elements in the document, including html, head and body. If the * selector is used together with another element, it selects all child elements within the specified element.

Which selects document root element?

The :root selector selects the document's root element. In HTML, the root element is always the <html> element.

How do you select a root in Javascript?

We can use the :root selector, which matches the root element of the document , with document. querySelector() in the following way to get the root element: console. log(document.


2 Answers

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); 
like image 53
adeneo Avatar answered Sep 21 '22 19:09

adeneo


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 
like image 41
GetFree Avatar answered Sep 22 '22 19:09

GetFree