7

I know there's a way to do this, but I cannot recall how.

How do I go about grabbing the HTML element (top-most element in the DOM), using plain JavaScript?

0

2 Answers 2

9

Use document.documentElement.

See the docs: https://developer.mozilla.org/en-US/docs/Web/API/Document/documentElement

Sign up to request clarification or add additional context in comments.

1 Comment

+1, added a link to docs. It should also be noted that, when using this for the purpose of determining the size of the entire page, there are variances between quirks mode and standards mode. See this discussion for more info: codingforums.com/showthread.php?t=138407#4
0
var html = document.getElementsByTagName('html')[0]; 

2 Comments

I like this way compared to the chosen answer, even though I thought of it already. Comparing them, I like how more explicit and clear this one is. A document.html would be even better, though.
I'm not sure if document.html would've worked when this was written six years ago

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.