You are here: Reference > JavaScript > client-side > HTML DOM > properties > nodeType

nodeType property

Browser support:
Returns an integer that indicates the type of the node.

Syntax:

object.nodeType;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Integer that represents the type of the node.
Predefined constants are available for the possible values of this property, in the scope of the Node object. You can use them directly through the Node interface (Node.ELEMENT_NODE) or through an instance of the Node object. Unfortunately, these constants are not supported by Internet Explorer before version 9, so use the numeric values instead of the predefined constants for a cross-browser solution.
The value can be one of the following values (the value of a predefined constant appears in parentheses after the constant):
ELEMENT_NODE (1)
9
Element node.
ATTRIBUTE_NODE (2)
9
Attribute node.
TEXT_NODE (3)
9
Text node.
CDATA_SECTION_NODE (4)
9
CDATA Section node.
ENTITY_REFERENCE_NODE (5)
9
ENTITY Reference node.
ENTITY_NODE (6)
9
ENTITY node.
PROCESSING_INSTRUCTION_NODE (7)
9
PROCESSING Instruction node.
COMMENT_NODE (8)
9
COMMENT node.
DOCUMENT_NODE (9)
9
DOCUMENT node.
DOCUMENT_TYPE_NODE (10)
9
DOCUMENT Type node.
DOCUMENT_FRAGMENT_NODE (11)
9
DOCUMENT Fragment node.
NOTATION_NODE (12)
9
NOTATION node.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the nodeType property:
<head>
    <script type="text/javascript">
        function GetNodeInfo () {
            var div = document.getElementById ("myDiv");
            var commentNode = div.firstChild;
            var message = "";
            message += "The name of the node: " + commentNode.nodeName + "\n";
            message += "The type of the node: " + commentNode.nodeType + "\n";
            message += "The value of the node: " + commentNode.nodeValue;
            alert (message);
        }
    </script>
</head>
<body>
    <div id="myDiv"><!-- this text is not rendered --></div>
    <button onclick="GetNodeInfo ()">Get node info!</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content