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. The DOM is tree - structured, with the document
object at the root. Each HTML element in the page is a node in this tree, and these nodes can have parent, child, and sibling relationships.
JavaScript can access and manipulate the DOM, enabling developers to create dynamic web pages. For example, JavaScript can change the text of a paragraph, add new elements to the page, or respond to user actions like clicks and keystrokes.
There are several ways to select DOM elements using JavaScript.
getElementById
// HTML
<!DOCTYPE html>
<html>
<body>
<p id="myParagraph">This is a paragraph.</p>
<script>
// Select the element with the given ID
const paragraph = document.getElementById('myParagraph');
console.log(paragraph.textContent);
</script>
</body>
</html>
querySelector
// HTML
<!DOCTYPE html>
<html>
<body>
<div class="myDiv">This is a div.</div>
<script>
// Select the first element that matches the CSS selector
const div = document.querySelector('.myDiv');
console.log(div.textContent);
</script>
</body>
</html>
Once an element is selected, its content can be modified.
// HTML
<!DOCTYPE html>
<html>
<body>
<p id="myParagraph">Old text</p>
<script>
const paragraph = document.getElementById('myParagraph');
// Change the text content
paragraph.textContent = 'New text';
</script>
</body>
</html>
Attributes of an element can also be changed.
// HTML
<!DOCTYPE html>
<html>
<body>
<img id="myImage" src="oldImage.jpg" alt="Old Image">
<script>
const image = document.getElementById('myImage');
// Change the src attribute
image.src = 'newImage.jpg';
</script>
</body>
</html>
New elements can be created and added to the DOM, and existing elements can be removed.
// HTML
<!DOCTYPE html>
<html>
<body>
<div id="myDiv"></div>
<script>
// Create a new paragraph element
const newParagraph = document.createElement('p');
newParagraph.textContent = 'This is a new paragraph.';
// Get the target div
const div = document.getElementById('myDiv');
// Append the new paragraph to the div
div.appendChild(newParagraph);
// Remove the new paragraph
div.removeChild(newParagraph);
</script>
</body>
</html>
Event handling is a crucial part of creating interactive web pages. JavaScript can listen for events like clicks, keypresses, etc., and execute code in response.
// HTML
<!DOCTYPE html>
<html>
<body>
<button id="myButton">Click me</button>
<script>
const button = document.getElementById('myButton');
button.addEventListener('click', function() {
alert('Button was clicked!');
});
</script>
</body>
</html>
Traversing the DOM means moving through the tree - like structure to access different elements.
// HTML
<!DOCTYPE html>
<html>
<body>
<ul id="myList">
<li>Item 1</li>
<li>Item 2</li>
</ul>
<script>
const list = document.getElementById('myList');
// Get the first child of the list
const firstItem = list.firstElementChild;
console.log(firstItem.textContent);
</script>
</body>
</html>
// Bad practice
const element = document.getElementById('myElement');
element.style.color = 'red';
element.style.fontSize = '20px';
// Good practice
const element = document.getElementById('myElement');
element.style.cssText = 'color: red; font - size: 20px;';
JavaScript and the DOM provide a powerful combination for creating dynamic and interactive web pages. By understanding the fundamental concepts, usage methods, common practices, and best practices, developers can efficiently interact with web pages, enhance user experience, and build complex web applications. Whether it’s simple text changes or complex animations, JavaScript and the DOM offer the tools needed to bring web pages to life.