Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

DOM (Document Object Model)

  • Difficulty Level : Easy
  • Last Updated : 04 Oct, 2021

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

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

HTML




<!DOCTYPE html>
<html>
 
<body>
  <h2>GeeksforGeeks</h2>
  <!--In this e.g. We find HTML Elements by Id in DOM -->
  <p id="intro">A Computer Science portal for geeks.</p>
 
 
   
<p>This example illustrates the <b>getElementsById</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:

In this post, we will discuss Document Object Model(DOM) along with its properties and methods used to manipulate Document.
 



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

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

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 DOM, we can easily access and manipulate tags, IDs, classes, Attributes, or Elements using commands or methods provided by the Document object.

Why DOM is 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, a corresponding document is created(DOM). DOM is basically the representation of the same HTML document but in a different format with the use of objects. Javascript interprets DOM easily i.e javascript can not understand the tags(<h1>H</h1>) in HTML document but can understand object h1 in DOM. Now, Javascript can access each of the objects (h1, p, etc) by using different functions.

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. 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 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 of 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. 
 



 

  1. Window Object: Window Object is always at top of the hierarchy.
  2. Document object: When an HTML document is loaded into a window, it becomes a document object.
  3. Form Object: It is represented by form tags.
  4. Link Object: It is represented by link tags.
  5. Anchor Object: It is represented by a href tags.
  6. Form Control Elements:: Form can have many control elements such as text fields, buttons, radio buttons, and checkboxes, etc.

Methods of Document Object:

  1. write(“string”): writes the given string on the document.
  2. getElementById(): returns the element having the given id value.
  3. getElementsByName(): returns all the elements having the given name value.
  4. getElementsByTagName(): returns all the elements having the given tag name.
  5. getElementsByClassName(): returns all the elements having the given class name.

Example

html




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

 
 

 
 

 

What DOM is not? 

  1. The Document Object Model is not a binary description where it does not define any binary source code in its interfaces.
  2. The Document Object Model is not used to describe objects in XML or HTML whereas the DOM describes XML and HTML documents as objects.
  3. The Document Object Model is not represented by a set of data structures; it is an interface that specifies object representation.
  4. The Document Object Model does not show the criticality of objects in documents i.e it doesn’t have information about which object in the document is appropriate to the context and which is not.

Levels of DOM

  1. Level 0: Provides a low-level set of interfaces.
  2. Level 1: DOM level 1 can be described in two parts: CORE and HTML.
    • CORE provides low-level interfaces that can be used to represent any structured document.
    • HTML provides high-level interfaces that can be used to represent HTML documents.
  3. Level 2 : consists of six specifications: CORE2, VIEWS, EVENTS, STYLE, TRAVERSAL, and RANGE.
    • CORE2: extends the functionality of CORE specified by DOM level 1.
    • VIEWS: views allows programs to dynamically access and manipulate the content of the document.
    • EVENTS: Events are scripts that are either executed by the browser when the user reacts to the web page.
    • STYLE: allows programs to dynamically access and manipulate the content of style sheets.
    • TRAVERSAL: allows programs to dynamically traverse the document.
    • RANGE: allows programs to dynamically identify a range of content in the document.
  4. Level 3: consists of five different specifications: CORE3, LOAD and SAVE, VALIDATION, EVENTS, and XPATH.
    • CORE3: extends the functionality of CORE specified by DOM level 2.
    • LOAD and SAVE: allows the program to dynamically load the content of the XML document into the DOM document and save the DOM Document into an XML document by serialization.
    • VALIDATION: allows the program to dynamically update the content and structure of the document while ensuring the document remains valid.
    • EVENTS: extends the functionality of Events specified by DOM Level 2.
    • XPATH: XPATH is a path language that can be used to access the DOM tree.

 Example of dom manipulation:

HTML




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

Output:

References:

  1. https://codescracker.com/js/js-dom-levels.htm
  2. https://www.w3.org/TR/DOM-Level-3-Core/introduction.html

HTML is the foundation of webpages, is used for webpage development by structuring websites and web apps.You can learn HTML from the ground up by following this HTML Tutorial and HTML Examples.




My Personal Notes arrow_drop_up
Recommended Articles
Page :