DOM Manipulation—Basics
Learn what the Document Object Model (DOM) is, as well as how JavaScript interacts with HTML code.
What is DOM?
DOM stands for Document Object Model. In simple terms, the DOM is a tree generated by the browsers to make the complete HTML code visible to the user. The DOM is a standard and language-independent interface that allows any scripting language, such as JavaScript, to modify the contents of a web page dynamically and efficiently by using the DOM tree. Each element or tag in HTML is converted to a node and is attached to its parent node (under which a tag is defined). Each DOM tree has a root node called "Document." Under this node, all the HTML nodes reside.
Understand DOM from HTML code
Let's look at the HTML code shown below, then look at the DOM tree of the HTML code.
<html><head><title>Welcome to the Course!</title></head><body><div><p>We are learning JavaScript</p></div></body></html>
The HTML code given above is a basic HTML code that displays a paragraph in the browser with the web page's title. Now let's see the DOM tree generated below:
Explanation:
Every DOM tree's root will be an object named
Document
.Using the tree's root, JavaScript can access any part of the web page by traversing the tree.
Every element (tag) and the text (even if there is a space or a newline character in the HTML code) is converted to a node in the tree. ...