Node Properties: Type, Tag and Contents

Mastering the Document Object Model (DOM) is essential for any web developer looking to manipulate web pages dynamically. This article delves deep into the core aspects of DOM node properties, providing practical code examples to enhance your JavaScript programming skills.

Understanding Node Types and Names

In JavaScript, the DOM tree comprises nodes of various types. Each node in the document is an object representing a part of the document. Nodes can be element nodes, text nodes, attribute nodes, etc. Here’s a brief overview:

  • Element nodes: Represent HTML or XML tags.
  • Text nodes: Contain the text inside HTML elements.
  • Attribute nodes: Attributes of HTML elements (not used directly in modern JavaScript DOM).

Node Types

This code snippet helps you determine the type of a node, which is crucial when you are traversing the DOM and need to make decisions based on node types. The nodeType property is an integer. Each type of node is associated with a specific numeric value according to the DOM specification. Here are the most commonly used node types:

  • Element Node: Represented by the number 1. This type corresponds to HTML or XML elements.
  • Attribute Node: Represented by the number 2. These nodes are used to specify attributes of elements, though modern JavaScript typically interacts with attributes directly through methods like getAttribute() and setAttribute(), rather than treating them as nodes.
  • Text Node: Represented by the number 3. These are the text elements contained within document elements. Text nodes can contain actual text and are crucial for manipulating the text content of the HTML document.
  • Comment Node: Represented by the number 8. These nodes represent the comments in the HTML or XML files.
  • Document Node: Represented by the number 9. This node type represents the entire document (i.e., the root element, such as the HTML document itself).
<div id="example">Example node</div>
    const node = document.getElementById("example");
    node.innerHTML = 'Node type is: ' + node.nodeType;

Node Names

This example demonstrates how to use nodeName. It is a property showing the name of the tag you use in your HTML file.

<div id="example"></div>
    const element = document.getElementById('example');
    element.innerHTML = 'nodeName: ' + element.nodeName;

Manipulating Text Content

Modifying the text content of nodes is a frequent task. The textContent property is a powerful way to get or set the text content of a node and its descendants, ignoring HTML tags.

Code Example: Updating Text Content

This script shows how to update the text content of an element using textContent:

<div id="example"></div>
  const element = document.getElementById('example');
  element.textContent = 'Updated text content';


Understanding and manipulating DOM node properties is a fundamental skill for any JavaScript developer. By learning how to properly use properties like nodeName and textContent, you can effectively manage the elements on your web pages. These examples provide a solid foundation for performing common tasks involving DOM nodes.

Practice Your Knowledge

What does the 'nodeType' property indicate in a DOM node in JavaScript?

Quiz Time: Test Your Skills!

Ready to challenge what you've learned? Dive into our interactive quizzes for a deeper understanding and a fun way to reinforce your knowledge.

Do you find this helpful?