I can select the body and html parts of the document using
$('body')
and
$('html')
respectively, but how do I select the document root?
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.
The :root selector selects the document's root element. In HTML, the root element is always the <html> element.
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.
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With