Get the node type of the body element:
var x = document.getElementById("myP").nodeType;
<!DOCTYPE html> <html> <body> <p id="myP">Click the button to get the node type of this element.</p> <button onclick="myFunction()">Test</button> <p id="demo"></p> <script> function myFunction() {// w w w. j a v a 2s . c om var x = document.getElementById("myP").nodeType; document.getElementById("demo").innerHTML = x; } </script> </body> </html>
The nodeType property returns the node type, as a number, of the specified node.
Item | Return |
---|---|
an element node | the nodeType property will return 1. |
an attribute node | the nodeType property will return 2. |
a text node | the nodeType property will return 3. |
a comment node | the nodeType property will return 8. |
This property is read-only.
Documents, elements, attributes, and other aspects of an HTML or XML document has different node types.
There are 12 different node types, which may have children of various node types:
Item | Node type | Children | Description |
---|---|---|---|
1 | Element | Element, Represents an element Text, Comment, ProcessingInstruction, CDATASection, EntityReference | |
2 3 4 | Attr Text CDATASection | Text, EntityReference Represents an attribute None Represents textual content in an element or attribute None Represents a CDATA section in a document | |
5 | EntityReference | Element Represents an entity reference ProcessingInstruction, Comment, Text, CDATASection, EntityReference | |
6 | Entity | Element, Represents an entity ProcessingInstruction, Comment, Text, CDATASection, EntityReference | |
7 8 9 | ProcessingInstruction None Represents a processing instruction Comment None Represents a comment Document Element, Represents the entire document (the root-node of the DOM tree) ProcessingInstruction, Comment, DocumentType | ||
10 | DocumentType | None | Provides an interface to the entities defined for the document |
11 | DocumentFragment | Element, Represents a Document object for a portion of a document ProcessingInstruction, Comment, Text, CDATASection, EntityReference | |
12 | Notation | None | Represents a notation declared in the DTD |
Node Types - Return Values
The return value of the nodeName and the nodeValue properties for each node type:
Item | Node type | nodeName returns | nodeValue returns |
---|---|---|---|
1 | Element | element name | null |
2 | Attr | attribute name | attribute value |
3 | Text | #text | content of node |
4 | CDATASection | #cdata-section | content of node |
5 | EntityReference | entity reference name | null |
6 | Entity | entity name | null |
7 | ProcessingInstruction | target | content of node |
8 | Comment | #comment | comment text |
9 | Document | #document | null |
10 | DocumentType | doctype name | null |
11 | DocumentFragment | #document fragment | null |
12 | Notation | notation name | null |
NodeType - Named Constants
NodeType | Named Constant |
---|---|
1 | ELEMENT_NODE |
2 | ATTRIBUTE_NODE |
3 | TEXT_NODE |
4 | CDATA_SECTION_NODE |
5 | ENTITY_REFERENCE_NODE |
6 | ENTITY_NODE |
7 | PROCESSING_INSTRUCTION_NODE |
8 | COMMENT_NODE |
9 | DOCUMENT_NODE |
10 | DOCUMENT_TYPE_NODE |
11 | DOCUMENT_FRAGMENT_NODE |
12 | NOTATION_NODE |