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. With JavaScript, you can interact with and manipulate the DOM to create dynamic and interactive web pages. In this article, we’ll explore the structure of the DOM and show examples of how to access and modify elements within it.
The DOM represents an HTML document as a tree structure, where each node in the tree is an object representing a part of the document. There are different types of nodes:
<div>
, <h1>
).class
, id
).Consider the following HTML structure:
<!DOCTYPE html> <html> <body> <div id="container"> <h1>Hello, World!</h1> <p>This is a paragraph.</p> </div> </body> </html>
In the DOM, this HTML document would be represented as a tree structure with nested nodes for each element, text, and attribute.
JavaScript provides several methods to access elements in the DOM. Here are some of the most commonly used methods:
The getElementById()
method allows you to access an element by its unique id
attribute.
let container = document.getElementById("container"); console.log(container); // Outputs the <div> element with id="container"
The getElementsByClassName()
method returns a collection of elements that have a specified class name.
let items = document.getElementsByClassName("item"); console.log(items); // Outputs a collection of elements with class "item"
The getElementsByTagName()
method returns a collection of elements with a specified tag name.
let paragraphs = document.getElementsByTagName("p"); console.log(paragraphs); // Outputs all <p> elements
The querySelector()
method returns the first element that matches a specified CSS selector, while querySelectorAll()
returns all elements that match the selector.
let header = document.querySelector("h1"); let paragraphs = document.querySelectorAll("p"); console.log(header); // Outputs the first <h1> element console.log(paragraphs); // Outputs all <p> elements
Once you have accessed an element, you can modify its content, attributes, and styles.
You can change the inner content of an element using the innerHTML
or textContent
properties.
let header = document.querySelector("h1"); header.innerHTML = "Welcome to the DOM!"; // Changes <h1> content to "Welcome to the DOM!"
innerHTML
allows you to set HTML content inside the element, while textContent
only sets text content without interpreting HTML tags.
To modify an attribute, use the setAttribute()
method or directly set the property:
let container = document.getElementById("container"); container.setAttribute("class", "new-container"); // Adds or changes the "class" attribute to "new-container"
You can also use removeAttribute()
to remove an attribute:
container.removeAttribute("class");
JavaScript allows you to create new elements and append them to the DOM.
let newParagraph = document.createElement("p"); newParagraph.textContent = "This is a new paragraph."; document.body.appendChild(newParagraph);
In this example, we create a new <p>
element, set its text content, and add it to the end of the <body>
element.
To remove an element from the DOM, you can use the removeChild()
method or the remove()
method.
let container = document.getElementById("container"); let header = document.querySelector("h1"); container.removeChild(header); // Removes the <h1> element from container
You can also remove an element directly by calling remove()
:
let paragraph = document.querySelector("p"); paragraph.remove(); // Removes the first <p> element
Event listeners allow you to respond to user interactions such as clicks, keypresses, or mouse movements. You can add event listeners to DOM elements using addEventListener()
.
let button = document.createElement("button"); button.textContent = "Click Me!"; document.body.appendChild(button); button.addEventListener("click", function() { alert("Button clicked!"); });
In this example, we create a <button>
element, add it to the <body>
, and attach a click
event listener to display an alert when the button is clicked.
The Document Object Model (DOM) is essential for creating dynamic web pages. Understanding how to access, modify, and manipulate DOM elements with JavaScript is a foundational skill for front-end development. By mastering these concepts, you can build interactive and responsive websites.