The Document Object Model (DOM) is essential for anyone looking to become proficient in frontend development. This lesson introduces the DOM as the core programming interface for web documents, enabling dynamic interaction with the webpage through JavaScript.
getElementById
and querySelector
.The Document Object Model (DOM) is a programming interface for web documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as a tree of nodes and objects, making it possible for programming languages like JavaScript to interact with the webpage.
When a web page is loaded, the browser creates a DOM of the page. The DOM model is constructed as a tree of Objects:
<div>
, <h1>
, <p>
, etc., become element nodes of the treeclass
, id
, and style
become attribute nodesUnderstanding this structure is crucial as it allows you to navigate and manipulate the page effectively.
Let’s explore some fundamental operations that you can perform on the DOM using JavaScript:
JavaScript provides several methods to select and access elements. Here are a few:
getElementById(id)
: Selects an element by its ID.getElementsByClassName(className)
: Selects all elements that have a specific class name.getElementsByTagName(tagName)
: Selects elements by their tag name.querySelector(selector)
: Uses CSS selectors to find the first matching element.querySelectorAll(selector)
: Returns all elements that match a specific CSS selector.Example: Accessing elements
<div id="intro">Welcome to our site!</div>
<div class="highlight">This is highlighted text.</div>
const intro = document.getElementById('intro');
console.log(intro.textContent); // Outputs: Welcome to our site!
const highlights = document.getElementsByClassName('highlight');
console.log(highlights[0].textContent); // Outputs: This is highlighted text.
You can also manipulate the DOM elements using JavaScript. This includes changing the text content, attributes, and styles.
textContent
: Sets or returns the text content of the specified nodeinnerHTML
: Sets or returns the HTML content inside an elementsetAttribute(name, value)
: Adds a new attribute or changes the value of an existing attribute on an elementExample: Manipulating elements
// Changing text content
intro.textContent = 'Hello, check out our features!';
// Adding HTML content
intro.innerHTML = '<strong>Hello, check out our features!</strong>';
// Changing attributes
intro.setAttribute('class', 'welcome');
JavaScript allows you to dynamically create new elements and remove existing ones.
createElement(tagName)
: Creates a new elementappendChild(child)
: Appends a child node to a parent noderemoveChild(child)
: Removes a child node from the DOMExample: Creating and appending elements
// Creating a new paragraph
const para = document.createElement('p');
para.textContent = 'Learn more about our services.';
document.body.appendChild(para); // Appending to the body of the document
Events are actions or occurrences that happen in the system you are programming, which the system tells you about so your code can react to them. For example, when a user clicks a button, hovering over a link, resizing the window, etc., these trigger events that you can respond to.
Adding Event Listeners
addEventListener(event, function)
: Sets up a function that will be called whenever the specified event is delivered to the target.Example: Handling click events
<button id="clickMe">Click me!</button>
document.getElementById('clickMe').addEventListener('click', function() {
alert('Button was clicked!');
});
Let’s combine what we’ve learned into a simple interactive webpage example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Interactive Webpage Example</title>
</head>
<body>
<h1 id="greeting">Hello, visitor!</h1>
<button id="changeText">Change greeting</button>
<script>
document.getElementById('changeText').addEventListener('click', function() {
const greeting = document.getElementById('greeting');
greeting.textContent = 'Welcome to our wonderful interactive page!';
});
</script>
</body>
</html>
Understanding and manipulating the DOM is fundamental for creating interactive web pages. This lesson has introduced you to the basics of navigating the DOM tree, accessing and modifying elements, and responding to user events. Practice these skills by incorporating them into small projects and gradually increasing the complexity of tasks you manage with the DOM.
By mastering DOM manipulation, you’re well on your way to becoming proficient in frontend development, enabling you to build responsive, dynamic user experiences.