Open In App

HTML DOM (Document Object Model)

HTML DOM, allows JavaScript to access and modify all the elements in an HTML document.

In this article, we will discuss DOM along with its properties and methods used to manipulate documents and understand their implementation through the examples.



Note: It is called a Logical structure because DOM doesn’t specify any relationship between objects. 

What is DOM?

The Document Object Model (DOM) is a programming interface for HTML(HyperText Markup Language) and XML(Extensible Markup Language) documents. It defines the logical structure of documents and the way a document is accessed and manipulated.

So basically Document Object Model is an API that represents and interacts with HTML or XML documents.

The DOM is a W3C (World Wide Web Consortium) standard and it defines a standard for accessing documents.

The W3C Dom standard is divided into three different parts:

HTML DOM

HTML DOM is a standard object model and programming interface for HTML documents. HTML DOM is a way to represent the webpage in a structured hierarchical way so that it will become easier for programmers and users to glide through the document.

With HTML DOM, we can easily access and manipulate tags, IDs, classes, attributes, or elements of HTML using commands or methods provided by the document object.

Using DOM JavaScript we get access to HTML as well as CSS of the web page and can also modify the behavior of the HTML elements.

Why is DOM Required?

HTML is used to structure the web pages and Javascript is used to add behavior to our web pages. When an HTML file is loaded into the browser, the JavaScript can not understand the HTML document directly. So it interprets and interacts with the Document Object Model (DOM), which is created by the browser based on the HTML document.

DOM is basically the representation of the same HTML document but in a tree-like structure composed of objects. JavaScript can not understand the tags(<h1>H</h1>) in HTML document but can understand object h1 in DOM.

JavaScript interprets DOM easily, using it as a bridge to access and manipulate the elements. DOM Javascript allow access to each of the objects (h1, p, etc) by using different functions.

The Document Object Model (DOM) is essential in web development for several reasons:

Structure of DOM

DOM can be thought of as a Tree or Forest (more than one tree). The term structure model is sometimes used to describe the tree-like representation of a document.  

Each branch of the tree ends in a node, and each node contains objects  Event listeners can be added to nodes and triggered on an occurrence of a given event. One important property of DOM structure models is structural isomorphism: if any two DOM implementations are used to create a representation of the same document, they will create the same structure model, with precisely the same objects and relationships.

Why DOM is called an Object Model?

Documents are modeled using objects, and the model includes not only the structure of a document but also the behavior of a document and the objects of which it is composed like tag elements with attributes in HTML.

Properties of DOM

Let’s see the properties of the document object that can be accessed and modified by the document object.

Representation of the DOM

Methods of Document Object

DOM provides various methods that allows users to interact with and manipulate the document. Some commonly used DOM methods are:

Example

In this example, We use HTML element id to find the DOM HTML element.




<!DOCTYPE html>
<html>
  
<body>
    <h2>GeeksforGeeks</h2>
  
    <!-- Finding the HTML Elements by their Id in DOM -->
    <p id="intro">A Computer Science portal for geeks.</p>
    <p>This example illustrates the <b>getElementById</b> method.</p>
    <p id="demo"></p>
    <script>
        const element = document.getElementById("intro");
        document.getElementById("demo").innerHTML =
          "GeeksforGeeks introduction is: " + element.innerHTML;
    </script>
</body>
  
</html>

Output:

Getting the HTML element by getElementById() Method

Example

This example describes the representation of the HTML elements in the tree structure.




<table>
    <ROWS>
        <tr>
            <td>Car</td>
            <td>Scooter</td>
        </tr>
        <tr>
            <td>MotorBike</td>
            <td>Bus</td>
        </tr>
    </ROWS>
</table>


HTML elements in tree-like structure

What DOM is not? 

Levels of DOM

DOM consisted of multiple levels, each representing different aspect of the document.

Example

This example illustrates the dom-manipulation using getElementById() Method.




<!DOCTYPE html>
<html>
  
<head>
    <title>DOM manipulation</title>
</head>
  
<body>
    <label>Enter Value 1: </label>
    <input type="text" id="val1" />
    <br />
    <br />
    <label>Enter Value 2: </label>
    <input type=".text" id="val2" />
    <br />
    <button onclick="getAdd()">Click To Add</button>
    <p id="result"></p>
    <script type="text/javascript">
        function getAdd() {
              
            // Fetch the value of input with id val1
            const num1 = Number(document.getElementById("val1").value);
              
            // Fetch the value of input with id val2
            const num2 = Number(document.getElementById("val2").value);
            const add = num1 + num2;
            console.log(add);
              
            // Displays the result in paragraph using dom
            document.getElementById("result").innerHTML = "Addition : " + add;
              
            // Changes the color of paragraph tag with red
            document.getElementById("result").style.color = "red";
        }
    </script>
</body>
  
</html>

Output:

Manipulating the Document objects using getElementById() Method

Conclusion

DOM is a programming interface for web documents. It allows us to modify HTML or XML document using functions. In this guide, we specifically cover HTML DOM, that allows us to manipulate HTML tags, attributes or elements using JavaScript.

We have covered DOM, its properties, structure, methods with examples. Understanding DOM and it’s benefits allow developers to get complete idea of web development. Modify HTML and CSS elements with DOM JavaScript.

Frequently Asked Questions on DOM

What do you mean by DOM?

DOM stands for Document Object Model. It is a programming interface for web documents like HTML, XML and SVG documents. DOM represents elements of documents as tree of objects.

What is an example of a DOM?

DOM representation for a HTML code can be seen as:

Document

– HTML
– head
– title (text: “My Website”)
– body
– h1 (text: “Welcome!”)
– p (text: “This is the main content.”)

What is DOM in JavaScript?

DOM in JavaScript refers to a programming interface. It provides a way to access, manipulate, and interact with the structure and content of an HTML document.

Why DOM is used in JavaScript?

DOM is used in JavaScript for many purposes like: Dynamically style and update content, performing client-side validation, event handling, etc.


Article Tags :