Understanding Document Object Model (DOM)
August 7, 2020
A Document Object Model (abbreviated as DOM) is a structure which acts as the framework of a standard HTML (HyperText Markup Language) or DHTML (Dynamic HyperText Markup Language) webscript. DOM is basically a tree with many branches and sub-branches which describe various elements of the webpage. Each branch of the tree represents one element. Each element may be referred to as a single node in the DOM structure. If a particular element has one or more attributes in the HTML webpage, then the branch forks into multiple nodes depending on the number of attributes. The formation of nodes is proportional to the number of attributes in the HTML page.
DOM follows a hierarchical tree structure in its representation. The first node in the DOM is Document node. That is because the entire web code for the HTML page is nothing but a document. The
<html> tag is the root node, and all of the nodes which are directly attached to the root node are the child nodes.
Let’s take a look at this simple example:
<!DOCTYPE html> <html lang="en" dir="ltr"> <head> <title>This text displays the title of the web page</title> </head> <body> <h1>HELLO WORLD</h1> <p>The above text will be larger in size</p> </body> </html>
This is a simple example of the markup language with a few very basic tags. As you can see, the
<title> tag is inside the
<head> tag. The text, “This text displays the title of the web page” will be the title of the webpage in the header section. The main content of the webpage is what is inside the
<body> tag. DOM structure for the above HTML code will be:
<html> node, or the root element, acts as the parent node to
<body> nodes. The
<body> nodes act as parent nodes to
<p> nodes respectively. In the same way, the hierarchy continues if any more elements or attributes are introduced in the web script.
Having a DOM helps to make any relevant changes in the webpage without having to change the actual web code or the corresponding HTML page. DOM helps the user access the tree model. Once the access is granted, the user can make any changes related to the composition, layout, content, or style of the HTML page.
DOM is not a language, but rather a structural representation of web pages in the form of an Application Programming Interface (API). One of the common examples of accessing DOM is through the Inspect Element tool in web browsers. Using the console tool in the inspect element helps us to make relevant temporary changes to the web pages. The changes can be related to HTML and CSS files. For example, by typing
console.log(document.head); in the console tool, we can access the
<head> section of the current HTML page.
console.log(document.body); helps us to access the
<body> section. Not only elements or attributes, but we can also retrieve details of the webpage.
Consider this code,
console.log(document.documentURI);. (URI stands for Uniform Resource Identifier.) The
documentURI feature returns the location of the webpage. To make things simpler, all the pages have the same preset set of strings but differ in their extended hierarchical names. For example,
Some more examples of APIs used to manipulate web pages using DOM are:
- DOM is an easy way to learn how HTML web pages are interpreted by web browsers. The DOM structure explains how a particular web page is built hierarchically.
- By using DOM, one can utilize the prewritten HTML codes to study them. Web pages can be manipulated using scripting languages by changing the values of attributes of DOM elements.
- DOM acts as an interactive API where the user can design his/her own ideas by inspecting web pages and employing them in their own HTML codes. Different elements can be added or removed.
- CSS or Cascading Style Sheet is a type of markup language which is used to design front end applications. DOM helps the user understand and manipulate the styling of a particular webpage.
About the authorKanishkvardhan A N
Kanishkvardhan A N is a sophomore at SDM College of Engineering and Technology pursuing his B.E in Information Science and Engineering. Apart from coding, he is ardent about exploring stellar space and understanding astronomy. In his leisure, he likes to play the guitar and write short stories.