This page is Ready to Use

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

nodeType

Summary

Retrieves the type of the requested node.

Property of dom/Nodedom/Node

Syntax

Note: This property is read-only.

var nodeType = node.nodeType;

Return Value

Returns an object of type NumberNumber

One of the node type constants defined on Node, or null.

Node.ELEMENT_NODE =1

Node.ATTRIBUTE_NODE = 2

Node.TEXT_NODE = 3

Node.CDATA_SECTION_NODE = 4

Node.ENTITY_REFERENCE_NODE = 5

Node.ENTITY_NODE = 6

Node.PROCESSING_INSTRUCTION_NODE = 7

Node.COMMENT_NODE = 8

Node.DOCUMENT_NODE = 9

Node.DOCUMENT_TYPE_NODE = 10

Node.DOCUMENT_FRAGMENT_NODE = 11

Node.NOTATION_NODE = 12

Examples

This example assigns the nodeType property of the body object to a variable.

var iType = document.body.nodeType;

This example assigns the nodeType property of a node created with the createElement method to a variable.

var oNode = document.createElement("B");
document.body.insertBefore(oNode);
var iType = oNode.nodeType;

This example users the node constants to return the nodeType description.

function getNodeTypeName(nType){
   switch (nType){
        case Node.ELEMENT_NODE:
            return 'ELEMENT_NODE';
        case Node.ATTRIBUTE_NODE:
            return 'ATTRIBUTE_NODE';
        case Node.TEXT_NODE:
            return 'TEXT_NODE';
        case Node.CDATA_SECTION_NODE:
            return 'CDATA_SECTION_NODE';
        case Node.ENTITY_REFERENCE_NODE:
            return 'ENTITY_REFERENCE_NODE';
        case Node.ENTITY_NODE:
            return 'ENTITY_NODE';
        case Node.PROCESSING_INSTRUCTION_NODE:
            return 'PROCESSING_INSTRUCTION_NODE';
        case Node.COMMENT_NODE:
            return 'COMMENT_NODE';
        case Node.DOCUMENT_NODE:
            return 'DOCUMENT_NODE';
        case Node.DOCUMENT_TYPE_NODE:
            return 'DOCUMENT_TYPE_NODE';
        case Node.DOCUMENT_FRAGMENT_NODE:
            return 'DOCUMENT_FRAGMENT_NODE';
        case Node.NOTATION_NODE:
            return 'NOTATION_NODE';
        default:
            return 'unknown nodeType:'+ntype;
      }
   }

Notes

If the node represents an attribute retrieved from the attributes collection, the nodeType returns null.

The proprietary <comment> element is recognized as a HTMLUnknown element in IE10 and higher and returns ELEMENT_NODE as its type.

Use the HTML comment block instead.

<-- this is a hidden comment tag -->

<comment> this is a legacy IE comment tag and will display on your web page… do not use or remove from your legacy pages</comment>

Related specifications

DOM Level 3 Core
Recommendation

Attributions