HTML DOM nodeType Property
Example
Return the node type of the body element:
document.body.nodeType;
Output result:
1
Try it »
Definition and Usage
The nodeType property returns the node type.
If the node is an element node, the nodeType property returns 1.
If the node is an attribute node, the nodeType property returns 2.
If the node is a text node, the nodeType property returns 3.
If the node is a comment node, the nodeType property returns 8.
This property is read-only.
Browser support





All major browsers support the nodeType property
Syntax
node
.nodeType
Technical Details
| Return Value: | String, representing the node type of the node. |
|---|---|
| DOM version | Core Level 1 Node Object |
Node types
In an HTML or XML document, elements, attributes, etc. have different node types.
There are 12 different node types, and different node types can also have different child node types:
| Node types | Description | Child node |
|---|---|---|
| 1 | Element | An element |
| 2 | Attr | An attribute |
| 3 | Text | The text content or attribute of an element |
| 4 | CDATASection | A CDATA section of a document (the text will not be parsed by the parser) |
| 5 | Entity reference manual | Entity references |
| 6 | Entity | An entity |
| 7 | ProcessingInstruction | A processing instruction |
| 8 | Comment | A comment |
| 9 | Document | The entire document (the root node of the DOM tree) |
| 10 | DocumentType | Provides an interface for the document entity |
| 11 | DocumentFragment | Represents adjacent nodes and their subtrees. |
| 12 | Notation | Represents the declaration of a notation in a DTD |
Node type - return value
Return the node name and the node value property value for each node type:
| Node types | nodeName return value | nodeValue return value |
|---|---|---|
| 1 | Element | Element name |
| 2 | Attr | Attribute name |
| 3 | Text | #text |
| 4 | CDATASection | #cdata-section |
| 5 | Entity reference manual | Entity reference name |
| 6 | Entity | Entity name |
| 7 | ProcessingInstruction | target |
| 8 | Comment | #comment |
| 9 | Document | #document |
| 10 | DocumentType | doctype name |
| 11 | DocumentFragment | #document fragment |
| 12 | Notation | Notation name |
Node type - static variable name
| Node type | 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 |
Element object