1

I want to get the html element in Javascript. I can now use document.body.parentNode or document.getElementsByTagName("html"). But I think there should be a simple, "right" way to do it.

So, what's the right way to get the html element in Javascript?

I tried to use search engines, but I don't know how to tell them the html tag is different from any other HTML tag, so it yielded no result I wanted.

pj-yn9aq
  • 113
  • 3

3 Answers3

1

You can use:

document.documentElement

which points to the document's root html node.

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

m90
  • 11,434
  • 13
  • 62
  • 112
0

I'd say document.getElementsByTagName, querySelector etc. are all "right ways" to get reference to html tag, it doesn't get simpler than that.

simka
  • 851
  • 8
  • 9