What Exactly Is The DOM?
The DOM is a programming interface for web documents. It takes an HTML or XML document and creates a document object model of it as a tree structure with the element of a webpage in it represented as a node. These nodes can be changed in order to dynamically alter the content, the structure or the style of the page and therefore a mode of interactivity by allowing the modification in response to various user actions.The document is represented as an intricate hierarchy according to the standards determined by the DOM. The tree structure has a root level which is denoted as the document node which as a rule of thumb encapsulates the entire web page. This level has various other nodes as its children such as element nodes, text nodes and attribute nodes among numerous others. By performing actions on these nodes, a user is successfully able to change how their webpage looks and performs in its functions.
The HTML elements within the webpage can be switched and altered in terms of their attributes or content using various javascript methods.
Accessing DOM Elements
Getting the desired outcome will first require you to access the right HTML elements and then only the changes can be made. Numerous Javascript methods as well as functions assist in element selection in the DOM. From searching for the element using tag name or class or even id or a combination of elements or their relations, all is possible using the following methods.1. getElementById : Every web page has unique ids and this is how pages identify each other. Since in most cases Id's have to be unique on a page this specific method is super fast and easy.
getElementById Example
let element = document.getElementById("myElement");
getElementsByClassName Example
let elements = document.getElementsByClassName("myClass");
getElementsByTagName Example
let elements = document.getElementsByTagName("p");
querySelector Example
let element = document.querySelector(".myClass");
5. querySelectorAll : This method is similar to `querySelector`, except that instead of returning a single element, it returns a NodeList which contains all the elements that match the selector.
querySelectorAll Example
let elements = document.querySelectorAll("div.myClass");
Modifying HTML Elements
Once you have accessed an HTML element using JavaScript, you are free to change its content or modify its attributes. Element properties can be modified in several ways:1. Changing the Element's Text Content : You can change the inner text of an element with the use of the `textContent` property. This is mainly useful for dynamically changing paragraphs, headings or any text within divs.
Changing Text Content Example
element.textContent = "New content here!";
Changing HTML Content Example
element.innerHTML = " New HTML content! ";
Changing Attributes Example
element.setAttribute("class", "newClass");
let srcValue = element.getAttribute("src");
Changing Styles Example
element.style.color = "blue";
element.style.fontSize = "20px";
Creating and Inserting Elements
JavaScript enables you to have new elements of HTML created and integrated into the DOM on the go. This is especially relevant for while list appending new items or form fields addition that depend on the user actions.1. New Elements Creation : You can utilize the `document.createElement` method to create new elements in the Web form. For instance, to create a new `
` element, you can do the following.
2. Element Appending : After creating a new element, it can be appended to the Document Object Model (DOM) tree using the `appendChild` method. Appending is where the newly created element is added as the last child to a selected parent node.
3. Inserting Elements at a Particular Position : In case you want more flexibility regarding placing the newly added content, you can make use of methods like `insertBefore`. The `insertBefore` method inserts the new node before the reference node as a child of the specified parent node.
Creating New Elements Example
let newDiv = document.createElement("div");
Appending Elements Example
parentElement.appendChild(newDiv);
Inserting Elements Example
parentElement.insertBefore(newDiv, referenceElement);
Deleting Elements
Another operation that one can perform in the DOM is deleting an element. For instance, in JavaScript, there is a `removeChild` method that allows a user to remove elements from its parent. Otherwise, you can also use the method `remove` directly on the element that you want to remove.Removing Elements Example
parentElement.removeChild(element); // Removes the element from its parent
element.remove(); // Removes the element itself