The Document Object Model, or DOM for
short, is a platform and programming interface for HTML (HyperText Markup
Language) and XML (eXtensible Markup Language) documents. It provides a data
representation comprising all the objects, depicting the structure and content
of the document on the web. Every webpage has its own DOM that represents the
page so that programs can alter its structure, look, and content.
In simpler terms, when a browser loads a webpage, it creates a model of that page. This model is the DOM tree and is filed in the browser’s memory. It provides functionality globally to the document, including how to obtain the page details and create new elements in the document. Remember, DOM is neither a part of HTML nor JavaScript; it’s a separate set of rules.
With the HTML DOM, you can use JavaScript to build HTML documents, navigate their hierarchical structure, and add, modify, or delete elements and attributes or their content, and so on. Almost anything found in an HTML document can be accessed, changed, deleted, or added using the JavaScript with the help of HTML DOM.
To understand this more
clearly, let's consider the following simple HTML document:
Example
The
above HTML document can be represented by the following DOM tree:
Document node: This is added at the top of the tree and represents
the entire page in the browser. As stated above, it is the starting point in
the DOM tree; you need to navigate via the document node to access any other
node in your DOM tree.
Element nodes: All the HTML elements like heading tags (<h1>
to <h6>) and paragraph tags (<p>) in the page create an element
node in the tree. You use these nodes to gain access to the elements’ attributes
and text nodes.
Attribute
nodes: When the opening tags in the
HTML document contains attributes, the tree represents them as attribute nodes.
These are not the children of the element nodes but a part of them.
Text nodes: Once
you have access to the element node, you can reach the text content within that
element, stored inside the text nodes of the DOM tree. These nodes cannot have
child nodes. Thus, a text node always creates a new branch in the DOM tree, and
no further branches come out of it.
Document node: This is added at the top of the tree and represents the entire page in the browser. As stated above, it is the starting point in the DOM tree; you need to navigate via the document node to access any other node in your DOM tree.
Element nodes: All the HTML elements like heading tags (<h1> to <h6>) and paragraph tags (<p>) in the page create an element node in the tree. You use these nodes to gain access to the elements’ attributes and text nodes.
Attribute nodes: When the opening tags in the HTML document contains attributes, the tree represents them as attribute nodes. These are not the children of the element nodes but a part of them.
Text nodes: Once you have access to the element node, you can reach the text content within that element, stored inside the text nodes of the DOM tree. These nodes cannot have child nodes. Thus, a text node always creates a new branch in the DOM tree, and no further branches come out of it.
Working
with the DOM Tree in JavaScript DOM
To access and modify the DOM tree, you need to follow two steps:
·
Locate the node
representing the element you want.
·
Works with the node’s
content, child elements, and attributes.
We will
discuss the various properties and methods in this tutorial based on the DOM
tree given below.
Accessing
the Elements
DOM Queries are the methods
that find elements in the DOM tree. They may return one element or a collection
of elements in a NodeList. You can select any element you desire from the
NodeList with the help of an index number (starting with 0).
The code mentioned below is the HTML and CSS code, common to all the
examples. All you would need to do is add the JavaScript code you want to
execute.
Code:
Methods to
Select an Individual Element Node
Following are
the methods to select an individual element in the tree:
- getElementById(‘id’): Uses the unique
value of the element’s id attribute. The HTML must have an id attribute for the
method to select it.
For example – getElementById(‘one’)
- querySelector(‘css selector’): Uses a CSS
selector, returns the first matching element.
For example – querySelector(‘h1’)
The code below
combines these methods to add styling to the webpage.
JavaScript Code:
</script>
Output:
Methods to Select
Multiple Elements (NodeLists)
There are
three common ways to select multiple elements in the tree:
- getElementsByClassName(): Selects all the elements that have a specified value for the class attribute.
- getElementsByTagName(): Selects all the elements that have the specified tag names.
- querySelectorAll(): Uses a CSS
selector, returns all the matching elements.
For example – querySelectorAll(‘li.mandatory’)
The above
methods always return a NodeList, even if the list contains only a single
element. This fact is important to remember because you need to use square
brackets [ ] if you want to access any element in the NodeList. The following
code explains all the methods of returning a NodeList.
JavaScript Code:
Output:
Get/ update
Element Content
For Text only: You can access and update the text in the containing element (and its children) with the help of two properties:
textContent
document.getElementById('p').textContent; (accessing)document.getElementById('p1').textContent = newText; (updating)
- innerText
document.getElementById('p').innerText; (accessing)document.getElementById('p1').innerText = newText; (updating)
Both these properties replace the entire content of the element, including all the markup present. But you should avoid using innerText since it is not part of any standard. Also, this method slows the speed of content loading on the page.
For Text and Markup
The innerHTML property
gets the content of the element and returns it as one long string, including
the markup it contains. But you should be careful of where you use this
property since there are some security risks associated with it.
For example
document.getElementById('p').innerHTML; (accessing)document.getElementById('p1').innerHTML = newText; (updating)
The code below
shows the three properties clearly. Run the code on your browser, and see the
output on your own. Play with the code, try adding and removing things, and see
what happens. This will help you understand how these properties work.
Code:
Output:
Adding/
removing HTML Content
In the above
example, we added an HTML <strong> tag
in the page with the help of innerHTML property. To remove any text content,
all you need to do is store an empty string in the element.
document.getElementById(‘p3’).innerHTML
= “ ”; (empty string)
This will
remove the text nodes of the containing and its child elements from the HTML
content.
DOM Manipulation
Another
technique to add/remove HTML content is DOM Manipulation. To add HTML content
to your webpage, you need to follow the three steps discussed below:
·
Create a new element using createElement().
·
Give it content by creating a text node using createTextNode() and
adding it to the element using the appendChild() method.
·
Add the element to the DOM tree by finding the location and then
using appendChild().
To remove an
element from the webpage, the steps are as follows:
·
Store the element you want to remove in a variable.
·
Store the parent of that element in another variable.
·
Remove the element from its containing element using the removeChild() method.
Let’s
implement these methods with the help of a code. We will use the same HTML and
CSS code we discussed at the beginning of this tutorial. All you need to do is
change the JavaScript in your program. In this example, we will add a paragraph
<p> tag in the page, and remove the <h1> tag from it.
Code:
Output:
Traversing between Element Nodes
You have the facility to navigate between related element nodes. The techniques available for this purpose include:
· parentNode: Selects the parent of the current element node (returns a single element).
· previousSibling/ nextSibling: Selects the previous or next sibling from the tree.
· firstChild/ lastChild: Selects the first or last child of the current element.