Professional Documents
Culture Documents
2.2 Usage of Advanced DOM and Events
2.2 Usage of Advanced DOM and Events
2.2 Usage of Advanced DOM and Events
Learning outcome1:
CHAP XI:
Manipulation of Browser DOM –
Usage of Advanced DOM and events
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 Advanced DOM and events
1.1. Introduction
Advanced DOM and events allow you to create dynamic and interactive web pages that respond to
user input.
<div id="my-div"></div>
// Select the element with the id "my-div"
const myDiv = document.querySelector("#my-div");
// Create a new element with the tag name "p"
const newP = document.createElement("p");
// Add the new element to the document
document.body.appendChild(newP);
// Delete the element with the id "my-div"
document.body.removeChild(myDiv);
There are many different types of events that can occur in the DOM, such as click, mouseenter, and
scroll. Event handlers are functions that are called when an event occurs.
1. Mouse Events
These events are related to mouse actions. These includes bellow examples:
button.addEventListener('mouseover', function() {
console.log('Mouse over the button');
});
button.addEventListener('mouseout', function() {
console.log('Mouse left the button');
});
</script>
2. Keyboard Events
These events are related to keyboard actions. These includes bellow examples:
input.addEventListener('keyup', function(event) {
console.log('Key released:', event.key);
});
input.addEventListener('keypress', function(event) {
console.log('Key pressed and released:', event.key);
});
</script>
3. Form Events
These events are related to form elements. These includes bellow examples:
input.addEventListener('change', function() {
console.log('Input value changed and lost focus:', input.value);
});
</script>
The DOM can be traversed using the document.querySelectorAll() method, the Node.childNodes
property, and the Node.nextElementSibling property.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
</ul>
A tabbed component is a component that allows the user to switch between different tabs. The
following code shows how to build a tabbed component using the DOM: