CodeGym /Courses /Frontend SELF EN /Introduction to DOM

Introduction to DOM

Frontend SELF EN
Level 41 , Lesson 0
Available

1.1 Basic DOM Concepts

The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a document as a tree of nodes, where each node is a part of the document, such as an element, text, comment, and so on. Understanding the DOM and its nodes is a fundamental aspect of working with web technologies.

Key DOM Concepts

What is the DOM?

The DOM (Document Object Model) is a hierarchical structure that describes the content of an HTML document. It allows programs and scripts to dynamically access and modify the document's structure, style, and content.

Nodes and Elements

In the context of the DOM, all parts of a document are considered nodes. Nodes come in several types:

  1. Element nodes: represent HTML tags.
  2. Text nodes: represent the text inside element nodes.
  3. Comment nodes: represent comments in HTML.
  4. Document nodes: represent the document itself.

Node Tree

The DOM is a tree-like structure where each node can have descendants (child nodes). The root node is the document object, which represents the entire HTML document.

Example of DOM Structure:

HTML
    
      <!DOCTYPE html>
      <html>
        <head>
          <title>Document Title</title>
        </head>
        <body>
          <h1>Hello, World!</h1>
          <p>This is a paragraph.</p>
        </body>
      </html>
    
  

For this document, the DOM tree will look like this:

Text
    
      document
      └── html
          ├── head
          │   └── title
          │       └── #text: "Document Title"
          └── body
              ├── h1
              │   └── #text: "Hello, World!"
              └── p
                  └── #text: "This is a paragraph."
    
  

1.2 Types of Nodes

1. Element Nodes

Element nodes correspond to HTML tags. For example, a node for the <body> tag is called an element node.

Example:

In this example, <body> and <h1> are element nodes.

HTML
    
      <body>
        <h1>Hello, World!</h1>
      </body>
    
  

2. Text Nodes

Text nodes contain the text inside element nodes. Text nodes are children of element nodes and contain string data.

Example:

In this example, the text "Hello, World!" is a text node, a child of the <h1> node.

HTML
    
      <h1>Hello, World!</h1>
    
  

3. Comment Nodes

Comment nodes represent comments in HTML, starting with <!-- and ending with -->.

Example:

In this example, <!-- This is a comment --> is a comment node.

HTML
    
      <!-- This is a comment -->

      <p>This is a paragraph.</p>
    
  

4. Document Nodes

The document node represents the root node of the DOM tree and is the parent node for all other nodes in the document.

Example:

The document object in JavaScript represents a document node:

JavaScript
    
      console.log(document);
    
  

1.3 Interacting with DOM Nodes

1. Getting Node Information

Node object properties allow you to get information about a specific node. For example, you can find out the node type, its name, and content.

Example:

HTML
    
      <html>
        <head>
          <title>My Website</title>
        </head>
        <body>
          <h1>Hello, World!</h1>
        </body>
      </html>
    
  
JavaScript
    
      const heading = document.querySelector('h1');

      console.log(heading.nodeName); // Output: H1
      console.log(heading.nodeType); // Output: 1 (Element node)
      console.log(heading.textContent); // Output: "Hello, World!"
    
  

2. Modifying Node Content

The content of nodes can be changed using DOM properties and methods.

Example:

JavaScript
    
      const paragraph = document.querySelector('p');
      paragraph.textContent = 'Updated text content';
    
  

3. Working with Child Nodes

Each DOM node can have child nodes that you can access and modify.

Example:

HTML
    
      <html>
        <head>
          <title>My Website</title>
        </head>
        <body>
          <ul>
            <li>About Us</li>
            <li>Career</li>
            <li>Contact</li>
          </ul>
        </body>
      </html>
    
  
JavaScript
    
      const list = document.querySelector('ul');
      const firstItem = list.firstElementChild;
      const lastItem = list.lastElementChild;

      console.log(firstItem.textContent); // Output the text of the first list item
      console.log(lastItem.textContent); // Output the text of the last list item
    
  

1.4 Useful Properties

1. Properties nodeName and nodeType

  • nodeName returns the name of a node
  • nodeType returns the type of a node (e.g., 1 for element nodes, 3 for text nodes)

Example:

HTML
    
      <html>
        <head>
          <title>My Website</title>
        </head>
        <body>
          <h1>Hello, World!</h1>
        </body>
      </html>
    
  
JavaScript
    
      const element = document.querySelector('h1');

      console.log(element.nodeName); // Output: H1
      console.log(element.nodeType); // Output: 1
    
  

2. Properties textContent and innerHTML

  • textContent returns or sets the textual content of a node
  • innerHTML returns or sets the HTML content of a node

Example:

HTML
    
      <html>
        <head>
          <title>My Website</title>
        </head>
        <body>
          <p>A really <b>interesting</b> and <b>useful</b> paragraph!</p>
        </body>
      </html>
    
  
JavaScript
    
      const element = document.querySelector('p');

      console.log(element.textContent); // Output the text content of the paragraph
      console.log(element.innerHTML); // Output the HTML content of the paragraph
    
  

3. Properties parentNode, childNodes, firstChild, and lastChild

  • parentNode returns the parent node
  • childNodes returns a collection of all child nodes
  • firstChild returns the first child node
  • lastChild returns the last child node

Example:

HTML
    
      <html>
        <head>
          <title>My Website</title>
        </head>
        <body>
          <ul>
            <li>About Us</li>
            <li>Career</li>
            <li>Contact</li>
          </ul>
        </body>
      </html>
    
  
JavaScript
    
      const element = document.querySelector('ul');

      console.log(element.parentNode); // Output the parent node
      console.log(element.childNodes); // Output the collection of child nodes
      console.log(element.firstChild); // Output the first child node
      console.log(element.lastChild); // Output the last child node
    
  
1
Task
Frontend SELF EN, level 41, lesson 0
Locked
First Paragraph
First Paragraph
1
Task
Frontend SELF EN, level 41, lesson 0
Locked
New List Item
New List Item
Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION