Professional Documents
Culture Documents
2.1 Usage of Document Object Model (DOM)
2.1 Usage of Document Object Model (DOM)
2.1 Usage of Document Object Model (DOM)
Learning outcome1:
CHAP X:
Manipulation of Browser DOM –
Usage of Document Object Model (DOM)
Table of Contents
1. Usage of Document Object Model (DOM).....................................................................................2
1.1. Introduction...........................................................................................................................2
1.2. DOM Tree..............................................................................................................................3
1.2.1. HTML DOM Tree of Objects...........................................................................................3
1.3. DOM Functions......................................................................................................................3
1.3.1. getElementById()...........................................................................................................4
1.3.2. querySelector()..............................................................................................................4
1.3.3. getElementsByClassName()...........................................................................................5
1.3.4. getElementsByTagName()..............................................................................................5
1.3.5. createElement().............................................................................................................6
1.4. JavaScript DOM for HTML and CSS........................................................................................6
1.4.1. JavaScript DOM for HTML..............................................................................................6
1.4.2. JavaScript DOM for CSS..................................................................................................7
1.5. Inner HTML and Inner Text Properties...................................................................................8
1.6. Exercises................................................................................................................................8
1. Usage of Document Object Model (DOM)
1.1. Introduction
The Document Object Model (DOM) is a programming interface for web documents. It represents
the structure of HTML, XML, or XHTML documents, enabling dynamic access and manipulation of
content using scripting languages like JavaScript.
According to the Document Object Model (DOM), every HTML tag is an object. Nested tags are
“children” of the enclosing one. The text inside a tag is an object as well.
All these objects are accessible using JavaScript, and we can use them to modify the page.
For example, document.body is the object representing the <body> tag.Running this code will make
the <body> red for 3 seconds:
With the object model, JavaScript gets all the power it needs to create dynamic HTML:
DOM Functions are JavaScript methods that allow you to interact with the DOM
1.3.1. getElementById()
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<div id="myDiv">Hello, World!</div>
<script>
const element = document.getElementById('myDiv');
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
JavaScript
</head>DOM for HTML and CSS
<body>
Inner HTML and Inner Text Properties
<div class="myClass">Hello, World!</div>
<script>
const element = document.querySelector('.myClass');
console.log(element.textContent); // Output: "Hello, World!"
</script>
</body>
</html>
1.3.3. getElementsByClassName()
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<div class="myClass">Hello</div>
<div class="myClass">World</div>
<script>
const elements = document.getElementsByClassName('myClass');
for (const element of elements) {
console.log(element.textContent);
}
// Output: "Hello"
// Output: "World"
</script>
</body>
</html>
1.3.4. getElementsByTagName()
console.log(element.textContent);
}
// Output: "Paragraph 1"
// Output: "Paragraph 2"
</script>
</body>
</html>
1.3.5. createElement()
<!DOCTYPE html>
<html>
<head>
<title>DOM Example</title>
</head>
<body>
<div id="container"></div>
<script>
const newElement = document.createElement('p');
newElement.textContent = 'This is a new paragraph';
const container = document.getElementById('container');
container.appendChild(newElement);
</script>
</body>
</html>
1.4. JavaScript DOM for HTML and CSS
The HTML DOM allows JavaScript to change the content of HTML elements. The easiest way to
modify the content of an HTML element is by using the innerHTML property.
<html>
<body>
<p id="p1">Hello World!</p>
<script>
document.getElementById("p1").innerHTML = "New text!";
</script>
</body>
</html>
<html>
<body>
<script>
document.getElementById("demo").innerHTML = "Date : " + Date();
</script>
</body>
</html>
In JavaScript, document.write() can be used to write directly to the HTML output stream:
<html>
<body>
<p>Bla bla bla</p>
<script>
document.write(Date());
</script>
<p>Bla bla bla</p>
</body>
</html>
Notes: Never use document.write() after the document is loaded. It will overwrite the document.
1.4.2. JavaScript DOM for CSS
The HTML DOM allows JavaScript to change the style of HTML elements. To change the style of an
HTML element, use this syntax:
<html>
<body>
<p id="p2">Hello World!</p>
<script>
document.getElementById("p2").style.color = "blue";
</script>
</body>
</html>
Two commonly used properties for manipulating content within HTML elements are innerHTML and
innerText.
innerHTML returns the text content of an element, including all spacing and inner HTML
tags.
innerText returns just the text content of the element, without CSS hidden text spacing and
tags, except <script> and <style> elements
1.6. Exercises
The DOM Tree stands for Document Object Model Tree. It represents the hierarchical structure of an
HTML/XML document, where each element is a node in the tree
You can use the value property to get the value of an input element
7. How can you change the CSS style of an element using JavaScript?
You can modify the style property of an element to change its CSS styles.
8. How can you set the HTML content of an element using innerHTML?
You can set the HTML content of an element using the innerHTML property.
You can retrieve the text content of an element using the innerText property.
The innerHTML property represents the entire HTML content of an element, including the element's
tag name, attributes, and child nodes. The innerText property represents the text content of an
element, excluding the element's tag name and attributes.