Introduction to the DOM Document
The Document Object Model (DOM) is a programming interface for web documents. It represents the structure of a web page as a tree of objects, where each node is an object representing a part of the page, such as elements, attributes, and text. The DOM Document is the root of this tree and provides methods to access and manipulate the page’s content.
What is the DOM Document?
The document
object in JavaScript is the entry point to the DOM. It represents the entire HTML or XML document loaded in the browser. Using the document
object, developers can read, modify, or create HTML elements dynamically.
Accessing DOM Elements
One of the primary uses of the document
object is to access elements in the page. Here are some common methods:
getElementById(id)
– Returns a single element with the specified ID. javascriptCopyEditvar heading = document.getElementById("main-heading");
getElementsByClassName(className)
– Returns a live HTMLCollection of all elements with the given class name. javascriptCopyEditvar items = document.getElementsByClassName("menu-item");
getElementsByTagName(tagName)
– Returns all elements with the given tag name. javascriptCopyEditvar paragraphs = document.getElementsByTagName("p");
querySelector(selector)
– Returns the first element that matches a CSS selector. javascriptCopyEditvar firstLink = document.querySelector("a");
querySelectorAll(selector)
– Returns all matching elements as a NodeList. javascriptCopyEditvar allButtons = document.querySelectorAll(".btn");
Modifying the DOM
You can change the content and attributes of elements using the DOM:
- Change inner content: javascriptCopyEdit
var para = document.getElementById("info"); para.innerHTML = "Updated content!";
- Change attributes: javascriptCopyEdit
var link = document.querySelector("a"); link.href = "https://example.com";
- Change styles: javascriptCopyEdit
link.style.color = "red";
Creating and Inserting Elements
You can also create new elements and add them to the document:
javascriptCopyEditvar newDiv = document.createElement("div");
newDiv.textContent = "Hello World!";
document.body.appendChild(newDiv);
To insert an element at a specific position:
javascriptCopyEditvar reference = document.getElementById("ref");
document.body.insertBefore(newDiv, reference);
Removing Elements
To remove an element:
javascriptCopyEditvar toRemove = document.getElementById("old");
toRemove.remove(); // Modern method
Or:
javascriptCopyEdittoRemove.parentNode.removeChild(toRemove); // Older method
DOM Events
The DOM allows you to respond to user interactions:
javascriptCopyEditvar btn = document.getElementById("clickMe");
btn.addEventListener("click", function() {
alert("Button clicked!");
});
Conclusion
The DOM document
object is essential for client-side web development. It enables developers to dynamically access and manipulate the content and structure of web pages. Whether you’re updating text, responding to user input, or building interactive interfaces, understanding the DOM is key to mastering JavaScript in the browser.