English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM nodeType attribute

HTML DOM Element Object

The nodeType read-only property returns the node type of the specified node in numeric form.

The nodeType attribute can be used to distinguish between different types of nodes, such as elements, text, and comments.

If the node is an element node, the nodeType attribute will return1.

If the node is an attribute node, the nodeType attribute will return2.

If the node is a text node, the nodeType attribute will return3.

If the node is a comment node, the nodeType attribute will return8.

Syntax:

node.nodeType
var x = document.getElementById("myPara").nodeType;
Test and See‹/›

Browser Compatibility

All browsers fully support the nodeType attribute:

Attribute
nodeTypeIsIsIsIsIs

Technical Details

Return value:A number representing the node type of the node
DOM Version:DOM Level1

Node Types (Node Types)

Documents, elements, attributes, and other aspects of HTML or XML documents have different node types.

Exists 12 There are different types of nodes, some of which may have child nodes of different types:

Node typeDescriptionChild nodes
1ElementRepresents elementsElement, Text, Comment, ProcessingInstruction, CDATASection, EntityReference
2AttrRepresents attributesText, EntityReference
3TextRepresents text content within an element or attribute.None
4CDATASectionRepresents the CDATA section in a document (text that will not be parsed by the parser).None
5EntityReferenceRepresents entity references.Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
6EntityRepresents entities.Element, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
7ProcessingInstructionRepresents processing instructions.None
8CommentRepresents comments.None
9DocumentRepresents the entire document (the root node of the DOM tree).Element, ProcessingInstruction, Comment, DocumentType
10DocumentTypeProvides an interface to entities defined for the documentNone
11DocumentFragmentRepresents a lightweight Document object that can contain a part of the documentElement, ProcessingInstruction, Comment, Text, CDATASection, EntityReference
12NotationRepresents the symbol declared in DTD.None

Node type - Return value

For each node type, the return values of nodeName and nodeValue properties are:

Node typenodeName returnsnodeValue returns
1ElementElement namenull
2AttrAttribute nameAttribute value
3Text#textThe content of the node
4CDATASection#cdata-sectionThe content of the node
5EntityReferenceEntity reference namenull
6EntityEntity namenull
7ProcessingInstructiontargetThe content of the node
8Comment#commentComment text
9Document#documentnull
10DocumentTypeDocument type namenull
11DocumentFragment#document fragmentnull
12NotationSymbol namenull

Node type-Named constant

Node typeNamed as a constant
1ELEMENT_NODE
2ATTRIBUTE_NODE
3TEXT_NODE
4CDATA_SECTION_NODE
5ENTITY_REFERENCE_NODE
6ENTITY_NODE
7PROCESSING_INSTRUCTION_NODE
8COMMENT_NODE
9DOCUMENT_NODE
10DOCUMENT_TYPE_NODE
11DOCUMENT_FRAGMENT_NODE
12NOTATION_NODE

More examples

This example checks if the first node within the document element is a comment node, and if not, it displays a message:

var node = document.documentElement.firstChild;
if (node.nodeType != Node.COMMENT_NODE) {
   alert("You should comment your code well!");
 }
Test and See‹/›

Return the node name, node type, and node value of the first child of the div element:

<div id="div-1">This is a div element.</div>
<script>
var x = document.getElementById("div-1).firstChild;
var txt = "";
txt += "The node name: " + x.nodeName + "<br>";
txt += "The node value: " + x.nodeValue + "<br>";
txt += "The node type: " + x.nodeType;
document.getElementById("para").innerHTML = txt;
</script>
Test and See‹/›

Related References

HTML DOM Reference:node .nodeName property

HTML DOM Reference:node .nodeValue property

HTML DOM Reference:node .childNodes property

HTML DOM Element Object