11 - JavaScript DOM
11 - JavaScript DOM
by : Emmersive Learning
Join us :
Telegram : https://t.me/EmmersiveLearning
Youtube : https://www.youtube.com/@EmmersiveLearning/
Document Object Model.
The Document Object Model (DOM) is a programming interface for HTML and
XML documents. It represents the page so that programs can change the
document structure, style, and content. JavaScript can interact with the DOM to
manipulate web pages dynamically.
What is the DOM?
<html>
<body>
<h1>Hello, World!</h1>
<p>This is a paragraph.</p>
</body>
</html>
Using JavaScript, you can manipulate this structure to dynamically add, remove,
or modify content on the web page.
2. getElementsByClassName()
○ Selects all elements with a specific class name. Returns a collection
(array-like object).
const elements =
document.getElementsByClassName('myClass');
3. getElementsByTagName()
○ Selects all elements with a specific tag name (like div, p, h1, etc.).
4. querySelector()
○ Selects the first element that matches a CSS selector (like #id,
.class, tag).
5. querySelectorAll()
○ Selects all elements that match a CSS selector. Returns a NodeList.
const elements = document.querySelectorAll('.myClass');
Once you've selected elements, you can manipulate them, such as changing
their content, style, or attributes.
1. Changing Content:
○ You can modify the inner HTML of an element using the
innerHTML property.
2. Changing Styles:
○ You can update the CSS styles of an element using the style
property.
3. Changing Attributes:
○ You can modify the attributes of an element using the
setAttribute() method.
1. Creating Elements:
○ You can create new HTML elements dynamically using the
createElement() method.
const newElement = document.createElement('p');
newElement.textContent = "This is a new paragraph.";
document.body.appendChild(newElement);
2. Removing Elements:
○ You can remove elements using the removeChild() or remove()
method.
Events allow JavaScript to interact with user actions, such as clicking buttons,
typing, or hovering over elements. You can handle events using event listeners.
function showAlert() {
alert('Button clicked!');
}
<h1 id="heading">Welcome!</h1>
<button id="changeBtn">Change Heading</button>
<script>
const button = document.getElementById('changeBtn');
button.addEventListener('click', function() {
const heading = document.getElementById('heading');
heading.textContent = "Hello, JavaScript!";
heading.style.color = "blue";
});
</script>
</body>
</html>
In this example:
● When the button is clicked, the h1 element’s text and color are updated
using JavaScript.
Conclusion
JavaScript's interaction with the DOM allows developers to create dynamic and
interactive web pages. By understanding how to access and manipulate DOM
elements, handle events, and traverse the DOM tree, you can significantly
enhance user experiences on the web.
Would you like to try a DOM-based project, or dive deeper into any specific
aspect of the DOM?
1. document
2. document.body
3. document.head
4. element.id
5. element.className
6. element.classList
7. element.tagName
9. element.textContent
● Gets or sets the text content inside an element (ignores HTML tags).
● Example: document.querySelector('h1').textContent = 'New
Heading'.
10. element.style
11. element.attributes
12. element.childNodes
● Returns a NodeList of all child nodes, including text and comment nodes.
● Example: element.childNodes[0] gives the first child node.
13. element.children
● Returns an HTMLCollection of child elements (only elements, no text or
comment nodes).
● Example: element.children[1] gives the second child element.
15. element.parentNode
17. element.innerHTML
1. getElementById()
2. getElementsByClassName()
3. getElementsByTagName()
4. querySelector()
5. querySelectorAll()
6. createElement()
● Creates a new HTML element.
● Example: const newElement =
document.createElement('div').
7. createTextNode()
8. appendChild()
9. removeChild()
10. replaceChild()
11. setAttribute()
12. getAttribute()
● Gets the value of an attribute.
● Example: element.getAttribute('href').
13. removeAttribute()
14. addEventListener()
Example:
element.addEventListener('click', function() {
alert('Element clicked');
});
15. removeEventListener()
16. cloneNode()
17. insertBefore()
18. scrollIntoView()
19. focus()
20. blur()
1. nodeName: Returns the name of the node (tag name for elements).
2. nodeType: Returns the type of the node (1 for elements, 3 for text nodes).
3. nodeValue: Gets or sets the value of a text node or attribute.
4. appendChild(): Adds a node as the last child of a parent.
5. removeChild(): Removes a child node from the DOM.
6. cloneNode(): Creates a copy of the node (shallow or deep).
<!DOCTYPE html>
<html>
<head>
<title>DOM Manipulation Example</title>
</head>
<body>
<script>
// Select the button
const button = document.getElementById('changeText');
</body>
</html>
● This example shows how to select elements, modify their content, and
handle an event.
Happy Coding!
Mehammed Teshome