2

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.

4
  • Why you need to get th html tag? Commented Mar 25, 2018 at 8:50
  • use <html id='myHtml'> and document.getElementById('myHtml') Commented Mar 25, 2018 at 8:53
  • document.querySelector(':root') Commented Mar 25, 2018 at 8:55
  • @WaelAbbas It's because I was thinking about quine. Commented Mar 25, 2018 at 9:06

3 Answers 3

3

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

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

Comments

1

Chek this answer
https://stackoverflow.com/a/22873490/3134112
reference:
https://developer.mozilla.org/en-US/docs/Web/API/Document.documentElement.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.