Node.nodeType

  • Revision slug: Web/API/Node.nodeType
  • Revision title: Node.nodeType
  • Revision id: 476739
  • Created:
  • Creator: kscarfone
  • Is current revision? No
  • Comment Updated tags

Revision Content

{{DomRef}}

Summary

The read-only Node.nodeType property returns an unsigned short integer representing the type of the node.

Syntax

var type = node.nodeType;

type is an unsigned short with one of the following values:

Name Value
ELEMENT_NODE 1
ATTRIBUTE_NODE {{deprecated_inline()}} 2
TEXT_NODE 3
CDATA_SECTION_NODE {{deprecated_inline()}} 4
ENTITY_REFERENCE_NODE {{deprecated_inline()}} 5
ENTITY_NODE {{deprecated_inline()}} 6
PROCESSING_INSTRUCTION_NODE 7
COMMENT_NODE 8
DOCUMENT_NODE 9
DOCUMENT_TYPE_NODE 10
DOCUMENT_FRAGMENT_NODE 11
NOTATION_NODE {{deprecated_inline()}} 12

Example

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

var node = document.documentElement.firstChild;
if (node.nodeType != Node.COMMENT_NODE)
  console.log("You should comment your code well!");

Specification

Revision Source

<div>
  {{DomRef}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>The read-only <code><strong>Node.nodeType</strong></code> property returns an <code>unsigned short</code> integer representing the type of the node.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
var <var>type</var> = <var>node</var>.nodeType;
</pre>
<p><code>type</code> is an unsigned short with one of the following values:</p>
<table class="standard-table">
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Value</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>ELEMENT_NODE</code></td>
      <td><code>1</code></td>
    </tr>
    <tr>
      <td><code>ATTRIBUTE_NODE</code> {{deprecated_inline()}}</td>
      <td><code>2</code></td>
    </tr>
    <tr>
      <td><code>TEXT_NODE</code></td>
      <td><code>3</code></td>
    </tr>
    <tr>
      <td><code>CDATA_SECTION_NODE</code> {{deprecated_inline()}}</td>
      <td><code>4</code></td>
    </tr>
    <tr>
      <td><code>ENTITY_REFERENCE_NODE</code> {{deprecated_inline()}}</td>
      <td><code>5</code></td>
    </tr>
    <tr>
      <td><code>ENTITY_NODE</code> {{deprecated_inline()}}</td>
      <td><code>6</code></td>
    </tr>
    <tr>
      <td><code>PROCESSING_INSTRUCTION_NODE</code></td>
      <td><code>7</code></td>
    </tr>
    <tr>
      <td><code>COMMENT_NODE</code></td>
      <td><code>8</code></td>
    </tr>
    <tr>
      <td><code>DOCUMENT_NODE</code></td>
      <td><code>9</code></td>
    </tr>
    <tr>
      <td><code>DOCUMENT_TYPE_NODE</code></td>
      <td><code>10</code></td>
    </tr>
    <tr>
      <td><code>DOCUMENT_FRAGMENT_NODE</code></td>
      <td><code>11</code></td>
    </tr>
    <tr>
      <td><code>NOTATION_NODE</code> {{deprecated_inline()}}</td>
      <td><code>12</code></td>
    </tr>
  </tbody>
</table>
<h2 id="Example" name="Example">Example</h2>
<p>This example checks if the first node inside the document element is a comment node, and if it is not, displays a message.</p>
<pre class="brush: js">
var node = document.documentElement.firstChild;
if (node.nodeType != Node.COMMENT_NODE)
  console.log("You should comment your code well!");
</pre>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-111237558">DOM Level 2 Core: Node.nodeType</a></li>
  <li><a class="external" href="http://www.w3.org/TR/DOM-Level-3-Core/core.html#ID-111237558">DOM Level 3 Core: Node.nodeType</a></li>
  <li><a class="external" href="http://dom.spec.whatwg.org/#node">DOM Standard</a></li>
</ul>
Revert to this revision