DEV Community

Cover image for DOM (Document Object Model)
Rubiya Reba
Rubiya Reba

Posted on

DOM (Document Object Model)

DOM (Document Object Model): A system for interacting with web documents that represents the structure of a webpage as a tree of objects. Each element, attribute, and piece of text is an object that can be accessed and manipulated through code. The DOM allows developers to dynamically modify content, structure, and style, creating interactive and responsive websites. It acts as a bridge between the static content of a webpage and the dynamic actions that can be performed through JavaScript, enabling real-time updates to the page without requiring a full reload.

Key Functions of the DOM:

  1. Tree Structure:
    • The DOM (Document Object Model) is like a family tree for a web page. When a web page loads, the browser reads the HTML and turns it into a structured tree of elements. Each part of the page—like headings, paragraphs, buttons, and images—is a node.
  2. Element Access & Retrieval:
    • The DOM serves as a digital map, allowing developers to pinpoint specific parts of a webpage through methods like querySelector() or getElementById(). These methods act like a "search engine" for elements within the HTML structure.
  3. Dynamic Content Updates:
    • With the DOM, content is not fixed—it can be transformed in real-time. Using properties such as innerHTML and textContent, developers can instantly change what’s displayed on the page, whether it’s altering text, images, or other elements. 4.** Flexible Structure Modification:**
    • The DOM provides flexibility in altering the structure of a webpage. Developers can create, remove, or reorganize elements using functions like appendChild(), removeChild(), and replaceChild(), allowing the page's layout to adapt to new requirements.
  4. Event Management & Interaction:
    • A core feature of the DOM is its ability to connect user actions with webpage reactions. By attaching event listeners through methods like addEventListener(), developers can make the page interactive—triggering changes based on clicks, keystrokes, or hover actions.
  5. Real-time Style Changes:
    • The appearance of elements on the page is fluid in the DOM. Developers can adjust an element's styling on-the-fly using the style property, allowing for dynamic changes like color shifts, size alterations, or visibility toggling.
  6. DOM Navigation & Traversal:
    • Traversing the DOM structure is like navigating a tree. Using properties like parentNode and nextSibling, developers can move between related elements and interact with various parts of the document in a hierarchical way.
  7. Element Creation on Demand:
    • The DOM empowers developers to create new HTML elements dynamically, making web pages adaptable. By using createElement(), developers can generate elements (such as divs, buttons, or sections) and then seamlessly inject them into the document.
  8. Attribute Control:
    • The DOM allows precise control over an element's attributes. Whether it’s updating a link’s href or modifying an image’s src, methods like getAttribute() and setAttribute() give developers the ability to change an element's core properties.

Example

Consider this HTML:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>DOM-Play</title>
</head>
<body>
    <div class="main">
        <h1>Hello</h1>
    </div>
</body>
</html>
Enter fullscreen mode Exit fullscreen mode

The textual representation of DOM tree will look like this:
The textual representation of DOM tree for a simple HTML document
This structure is how a simple HTML page is represented in the DOM. The DOM tree helps browsers understand the structure of the page so it can be rendered correctly for users.

Summary

  • The Document node is the top-level container.
  • html contains everything.
  • Inside html, there are two child elements: head and body.
    • The head contains a <meta> tag and a <title> tag.
    • The body contains a div with a class of "main", and inside that, there is a heading <h1> tag with the text "Hello".

The graphical diagram of the DOM structure is shown below:

The graphical diagram of the DOM structure

This visually represents how the DOM is structured like a tree, with each element as a node connected to its parent. Here, <head>, <meta>, <title>, <body>, <div> and <h1> are element node. The “DOM-Play” and “Hello” are text node inside <title> and <h1> respectively. charset="UTF-8" is an attribute of <meta> and class="main" is an attribute of <div>.

Summary

  • Each node represents an element in the HTML document.
  • A text node contains only textual content which does not have any child nodes or attributes.
  • An attribute is a property of an element node that provides additional information about the element. It is not considered as a child node of an element.
  • The tree structure shows how elements are nested inside each other.
  • JavaScript can access and manipulate any node to change the page dynamically.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay