The read-only Node.nodeType
property is an integer that identifies what the node is. It distinguishes different kind of nodes from each other, such as elements
, text
and comments
.
Syntax
var type = node.nodeType;
Returns an integer which specifies the type of the node. Possible values are listed in Node type constants.
Constants
Node type constants
Constant | Value | Description |
---|---|---|
Node.ELEMENT_NODE
|
1
|
An Element node like <p> or <div> .
|
Node.ATTRIBUTE_NODE
|
2
|
An Attribute of an Element .
|
Node.TEXT_NODE
|
3
|
The actual Text inside an Element or Attr .
|
Node.CDATA_SECTION_NODE
|
4
|
A CDATASection , such as <!CDATAWeb/API/Node/…> .
|
Node.PROCESSING_INSTRUCTION_NODE
|
7
|
A ProcessingInstruction of an XML document, such as <?xml-stylesheet … ?> .
|
Node.COMMENT_NODE
|
8
|
A Comment node, such as <!-- … --> .
|
Node.DOCUMENT_NODE
|
9
|
A Document node.
|
Node.DOCUMENT_TYPE_NODE
|
10
|
A DocumentType node, such as <!DOCTYPE html> .
|
Node.DOCUMENT_FRAGMENT_NODE
|
11
|
A DocumentFragment node.
|
Deprecated node type constants '
The following constants have been deprecated and should not be used anymore.
Constant | Value | Description |
Node.ENTITY_REFERENCE_NODE
|
5 | An XML Entity Reference node, such as &foo; . Removed in DOM4.
|
Node.ENTITY_NODE
|
6 | An XML <!ENTITY …> node. Removed in DOM4.
|
Node.NOTATION_NODE
|
12 | An XML <!NOTATION …> node. Removed in DOM4.
|
Examples
Different types of nodes
document.nodeType === Node.DOCUMENT_NODE; // true
document.doctype.nodeType === Node.DOCUMENT_TYPE_NODE; // true
document.createDocumentFragment().nodeType === Node.DOCUMENT_FRAGMENT_NODE; // true
var p = document.createElement("p");
p.textContent = "Once upon a time…";
p.nodeType === Node.ELEMENT_NODE; // true
p.firstChild.nodeType === Node.TEXT_NODE; // true
Comments
This example checks if the first node inside the document element is a comment, and displays a message if not.
var node = document.documentElement.firstChild;
if (node.nodeType !== Node.COMMENT_NODE) {
console.warn("You should comment your code!");
}
Specifications
Specification | Status | Comment |
---|---|---|
DOMThe definition of 'Node.nodeType' in that specification. | Living Standard | Deprecated ATTRIBUTE_NODE , ENTITY_REFERENCE_NODE and NOTATION_NODE types.
|
Document Object Model (DOM) Level 3 Core SpecificationThe definition of 'Node.nodeType' in that specification. | Obsolete | No changes. |
Document Object Model (DOM) Level 2 Core SpecificationThe definition of 'Node.nodeType' in that specification. | Obsolete | No changes. |
Document Object Model (DOM) Level 1 SpecificationThe definition of 'Node.nodeType' in that specification. | Obsolete | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
nodeType
|
Chrome
Full support 1 |
Edge
Full support 12 |
Firefox
Full support 1 |
IE
Full support 6 |
Opera
Full support 7 |
Safari
Full support 1.1 |
WebView Android
Full support 1 |
Chrome Android
Full support 18 |
Firefox Android
Full support 4 |
Opera Android
Full support 10.1 |
Safari iOS
Full support 1 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
Node.nodeType by Mozilla Contributors is licensed under CC-BY-SA 2.5.