guy8214
guy8214

Reputation: 1115

Javascript DOM clarification

I'm learning how to appending DOM nodes with Javascript and have a little clarification question. Here's an example:

<html>
   <head>
   </head>
   <body>
   </body>
</html>

    document.childNodes[0]

So here, you would get the <head> node because it is the first child after the <html> tag. My question is can I always consider "document" to be the equivalent of the <html> tag or root node?

Upvotes: 2

Views: 78

Answers (4)

raina77ow
raina77ow

Reputation: 106385

I'd recommend studying the W3C DOM spec as well: even if some parts of it won't mean much to you, it might save you a plenty of time and efforts later. )

That's what's said about document here:

The Document interface represents the entire HTML or XML document. Conceptually, it is the root of the document tree, and provides the primary access to the document's data.

Upvotes: 1

jfriend00
jfriend00

Reputation: 707328

document.childNodes[0] is the <!DOCTYPE> node when I try it (though not sure if that's always the case).

document.documentElement is the <html> tag.

document.body is the <body> tag.

Upvotes: 0

Ed Heal
Ed Heal

Reputation: 60007

document.childNodes[0] will be the html tag in your example.

Upvotes: 0

kirilloid
kirilloid

Reputation: 14304

Root node for html is ... html, which can be get via document.documentElement

I'm not sure, it is supported in all modern browsers, though.

Upvotes: 0

Related Questions