Javascript DOM HTML Node nodeType Property

Introduction

Get the node type of the body element:

var x = document.getElementById("myP").nodeType;

View in separate window

<!DOCTYPE html>
<html>
<body>

<p id="myP">Click the button to get the node type of this element.</p>

<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {//  w w w. j  a  v a 2s  .  c  om
  var x = document.getElementById("myP").nodeType;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The nodeType property returns the node type, as a number, of the specified node.

ItemReturn
an element node the nodeType property will return 1.
an attribute nodethe nodeType property will return 2.
a text node the nodeType property will return 3.
a comment nodethe nodeType property will return 8.

This property is read-only.

Node Types

Documents, elements, attributes, and other aspects of an HTML or XML document has different node types.

There are 12 different node types, which may have children of various node types:

Item
Node type
Children
Description
1





Element





Element, Represents an element
Text,
Comment,
ProcessingInstruction,
CDATASection,
EntityReference
2
3
4
Attr
Text
CDATASection
Text, EntityReference Represents an attribute
None Represents textual content in an element or attribute
None Represents a CDATA section in a document
5





EntityReference





Element Represents an entity reference
ProcessingInstruction,
Comment,
Text,
CDATASection,
EntityReference
6





Entity





Element, Represents an entity
ProcessingInstruction,
Comment,
Text,
CDATASection,
EntityReference
7
8
9



ProcessingInstruction None Represents a processing instruction
Comment None Represents a comment
Document Element, Represents the entire document (the root-node of the DOM tree)
ProcessingInstruction,
Comment,
DocumentType
10
DocumentType
None
Provides an interface to the entities defined for the document
11





DocumentFragment





Element, Represents a Document object for a portion of a document
ProcessingInstruction,
Comment,
Text,
CDATASection,
EntityReference
12
Notation
None
Represents a notation declared in the DTD

Node Types - Return Values

The return value of the nodeName and the nodeValue properties for each node type:

Item Node type nodeName returns nodeValue returns
1 Element element name null
2 Attr attribute nameattribute value
3 Text #text content of node
4 CDATASection #cdata-sectioncontent of node
5 EntityReference entity reference name null
6 Entityentity name null
7 ProcessingInstruction targetcontent of node
8 Comment #comment comment text
9 Document #document null
10 DocumentType doctype name null
11 DocumentFragment #document fragmentnull
12 Notation notation name null

NodeType - Named Constants

NodeType Named Constant
1ELEMENT_NODE
2ATTRIBUTE_NODE
3TEXT_NODE
4CDATA_SECTION_NODE
5ENTITY_REFERENCE_NODE
6ENTITY_NODE
7PROCESSING_INSTRUCTION_NODE
8COMMENT_NODE
9DOCUMENT_NODE
10 DOCUMENT_TYPE_NODE
11 DOCUMENT_FRAGMENT_NODE
12 NOTATION_NODE



PreviousNext

Related