5

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?

Zoolander
  • 2,293
  • 5
  • 27
  • 37

2 Answers2

9

Use document.documentElement.

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

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
Digital Plane
  • 37,354
  • 7
  • 57
  • 59
  • +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: http://www.codingforums.com/showthread.php?t=138407#4 – Chris Baker Sep 02 '11 at 22:15
0
var html = document.getElementsByTagName('html')[0];
Major Productions
  • 5,914
  • 13
  • 70
  • 149
  • 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. – Adé Oct 04 '17 at 13:57
  • I'm not sure if `document.html` would've worked when this was written six years ago – Major Productions Oct 04 '17 at 16:22