Node.nodeType

  • Revision slug: DOM/Node.nodeType
  • Revision title: Node.nodeType
  • Revision id: 382583
  • Created:
  • Creator: freddyb
  • Is current revision? No
  • Comment

Revision Content

{{DomRef}}

Summary

Returns an integer code 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>Returns an integer code 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>
  <thead>
    <tr>
      <th scope="col">Name</th>
      <th scope="col">Value</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>ELEMENT_NODE</code></td>
      <td>1</td>
    </tr>
    <tr>
      <td><code>ATTRIBUTE_NODE</code>{{deprecated_inline()}}</td>
      <td>2</td>
    </tr>
    <tr>
      <td><code>TEXT_NODE</code></td>
      <td>3</td>
    </tr>
    <tr>
      <td><code>CDATA_SECTION_NODE</code>{{deprecated_inline()}}</td>
      <td>4</td>
    </tr>
    <tr>
      <td><code>ENTITY_REFERENCE_NODE</code>{{deprecated_inline()}}</td>
      <td>5</td>
    </tr>
    <tr>
      <td><code>ENTITY_NODE</code>{{deprecated_inline()}}</td>
      <td>6</td>
    </tr>
    <tr>
      <td><code>PROCESSING_INSTRUCTION_NODE</code></td>
      <td>7</td>
    </tr>
    <tr>
      <td><code>COMMENT_NODE</code></td>
      <td>8</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_NODE</code></td>
      <td>9</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_TYPE_NODE</code></td>
      <td>10</td>
    </tr>
    <tr>
      <td><code>DOCUMENT_FRAGMENT_NODE</code></td>
      <td>11</td>
    </tr>
    <tr>
      <td><code>NOTATION_NODE</code>{{deprecated_inline()}}</td>
      <td>12</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