Introduction to DOM
Tags: webdev, html, javascript, computerscience
Published: 2020-02-08 06:03:11

The Document Object Model(DOM) is an **application programming interface** for HTML and XML documents. It defines the logical structure of documents and how a document can be accessed and manipulated using it.

The DOM is an object-oriented representation of the web page. Documents are represented in DOM using nodes and objects which facilitates changing its structure, style, and content with a programming language like javascript. All HTML tags and even text inside tags are represented as objects so that they can be easily accessed and modified according to the requirement.

From the beginning, DOM is designed in such a way that it can be used with any programming language. In this series, we will be using Javascript for all operations using DOM.

## DOM Tree
The HTML Document, when parsed by the browser, is converted into DOM for all further operations. The DOM represents HTML Document as a tree structure of tags as shown below. For example:

```html
<!DOCTYPE HTML>
<html>
<head>
<title>DOM | Hackinbits</title>
</head>
<body>
<h1>Welcome to hackinbits<h1>
<p> Learn programming and technology in bits.<p>
</body>
</html>
```

> You can edit the example document and see the tree structure at this link [hixie.ch](http://software.hixie.ch/utilities/js/live-dom-viewer/?%3C!DOCTYPE%20HTML%3E%20%0A%3Chtml%3E%20%0A%3Chead%3E%20%0A%3Ctitle%3EDOM%20%7C%20Hackinbits%3C%2Ftitle%3E%20%0A%3C%2Fhead%3E%20%0A%3Cbody%3E%20%0A%3Ch1%3EWelcome%20to%20hackinbits%3C%2Fh1%3E%20%0A%3Cp%3E%20Learn%20programming%20and%20technology%20in%20bits.%3C%2Fp%3E%20%0A%3C%2Fbody%3E%20%0A%3C%2Fhtml%3E%20)

## Parsing of HTML Document by Browser

![DOM generation from raw data](https://firebasestorage.googleapis.com/v0/b/hackinbits.appspot.com/o/dom_parsing.png?alt=media&token=c919489e-2b18-4db5-9154-7afc5d059a5c)

Let's discuss briefly how the HTML document is parsed by the browser and DOM is generated. When the browser processes the HTML document, it performs the following steps:

----

![Recieved Raw Data](https://firebasestorage.googleapis.com/v0/b/hackinbits.appspot.com/o/dom_bytes.png?alt=media&token=27900a87-9eaa-4406-8fae-098aea6542b1)

----

1. Conversion: The browser first converts received data into individual characters based on specified character encoding of the document( ex.UTF-8).

----

![Characters obtained from Raw Data](https://firebasestorage.googleapis.com/v0/b/hackinbits.appspot.com/o/dom_char.png?alt=media&token=df7996c8-fd97-4534-9506-498d80418bc4)

----

2. Tokenizing: In the next step, browser read strings of characters obtained from the first step and convert them into distinct tokens as specified by the W3C standards; for example "<html>" is a token.

----

![Tokens generated from characters](https://firebasestorage.googleapis.com/v0/b/hackinbits.appspot.com/o/dom_tokens.png?alt=media&token=bc300b5d-801d-4865-b7ff-b5f6304c77d6)

----

3. Lexing: The tokens produced in the second step are converted into "objects", which define their properties and rules.

----

![Nodes](https://firebasestorage.googleapis.com/v0/b/hackinbits.appspot.com/o/dom_nodes.png?alt=media&token=942a0d79-cb66-486b-a242-76deb908def0)

----

4. DOM construction: The objects created in this way are then linked to a tree data structure which also captures the relationship between HTML tags as defined in the original document. For example, The HTML object is the parent of body object, the body object is the parent of paragraph object and so on.

----

![DOM Tree](https://firebasestorage.googleapis.com/v0/b/hackinbits.appspot.com/o/dom_tree.png?alt=media&token=6efb4b77-35dd-4a4b-9df8-43fba873963e)

----

The DOM generated by the above steps is used by the browser for all further processing.

In the next article, we will see the DOM tree in detail and how we can use javascript to modify the structure, content, and style of an HTML document.


**Originally published on [hackinbits.com](https://hackinbits.com)**.
If you liked this article, don't forget to upvote and recommend it. Feel free to ask any questions in the comments below. 

## Useful Resources
[DOM Introduction - MDN](https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction)

[DOM Specification - whatwg.org](https://dom.spec.whatwg.org/)

[DOM Introduction - w3.org](https://www.w3.org/TR/DOM-Level-2-Core/introduction.html)

## Recommended Reading
[Doctype and its purpose in HTML Document](https://hackinbits.com/blog/doctype-html-and-its-purpose-in-html-document)