The Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the structure of a document as a tree of objects, with each object (or "node") in the tree representing an element or a piece of text within the document.
JavaScript is commonly used to interact with the DOM, allowing you to dynamically change the content and layout of a web page.
Here are some common tasks you can perform with the DOM in JavaScript:
- Accessing elements: You can use
document.getElementById()to access an element by its ID,document.getElementsByTagName()to access all elements with a specific tag, anddocument.querySelector()to access an element that matches a CSS selector.
- Changing elements: Once you have access to an element, you can change its content using the
innerHTMLproperty, change its styling using thestyleproperty, and add or remove classes using theclassListproperty.
- Traversing the tree: The DOM tree is hierarchical, so you can move up and down the tree using properties like
parentNode,previousSibling, andnextSibling.
- Creating new elements: You can create new elements using the
document.createElement()method and add them to the page using theappendChild()method.
Here is a sample of Js DOM code:
//Accessing an element by ID var myHeading = document.getElementById("myHeading"); //Changing the content of an element myHeading.innerHTML = "Hello, World!"; // Changing the style of an element myHeading.style.color = "red"; // Adding a class to an element myHeading.classList.add("special"); // Creating a new element var newParagraph = document.createElement("p"); // Adding content to the new element newParagraph.innerHTML = "This is a new paragraph."; // Adding the new element to the page document.body.appendChild(newParagraph);
The DOM provides a wide range of functionalities for manipulating HTML and XML documents. Here are some of the main functionalities:
- Accessing elements: You can use
document.getElementById()to access an element by its ID,document.getElementsByTagName()to access all elements with a specific tag,document.getElementsByClassName()to access all elements with a specific class, anddocument.querySelector()anddocument.querySelectorAll()to access elements that match a CSS selector.
- Changing elements: Once you have access to an element, you can change its content using the
innerHTMLproperty, change its styling using thestyleproperty, and add or remove classes using theclassListproperty. You can also add and remove attributes usingsetAttribute(),getAttribute()andremoveAttribute()methods.
- Traversing the tree: The DOM tree is hierarchical, so you can move up and down the tree using properties like
parentNode,previousSibling, andnextSibling. You can also traverse through child nodes usingchildNodes,firstChild, andlastChildproperties.
- Creating new elements: You can create new elements using the
document.createElement()method and add them to the page using theappendChild()orinsertBefore()methods.
- Manipulating events: DOM allows you to attach and remove event listeners to elements, like click, hover, keypress, etc.
addEventListener()andremoveEventListener()methods are used to attach and remove events.
- Working with forms: DOM provides several methods and properties to work with form elements, like
getElementById(),getElementsByTagName(),getElementsByName(),value,checked,defaultValue,defaultChecked, and so on.
- Document properties: Some of the commonly used properties are
document.title(to change the title of the document),document.body(to access the body element of the document),document.URL(to get the current URL of the document),document.referrer(to get the URL of the document that loaded the current document).
- Document methods: Some of the commonly used methods are
document.write()(to write text or HTML to the document),document.open()(to open a new document),document.close()(to close the document),document.getElementsByName()(to get all elements that have a specified name)
- Managing forms: The DOM provides several methods for working with form elements, such as
form.submit()to submit a form,form.reset()to reset a form, andform.elementsto access all of the form's elements as a collection. You can also useform.checkValidity()to check if the form is valid, andform.reportValidity()to show validation messages.
- Manipulating nodes: You can use the DOM to manipulate the structure of a document by adding, removing, and moving nodes. The
appendChild()method can be used to add a new node to the end of a parent node's children, while theinsertBefore()method can be used to insert a node before a specific child node. TheremoveChild()method can be used to remove a node from its parent, andreplaceChild()method can be used to replace a child node.
- Getting information about nodes: The DOM provides several properties and methods for getting information about nodes, such as
node.nodeNamewhich returns the name of the node,node.nodeTypewhich returns the type of the node,node.parentNodewhich returns the parent node of the current node andnode.childNodesreturns a collection of the child nodes of the current node.
document.forms: This property allows you to access all the forms in a document. You can use thedocument.formsproperty to access the forms by their index, e.g.,document.forms[0]to access the first form,document.forms[1]to access the second form, etc.
document.images: This property allows you to access all the images in a document. You can use thedocument.imagesproperty to access the images by their index, e.g.,document.images[0]to access the first image,document.images[1]to access the second image, etc.
document.links: This property allows you to access all the links in a document. You can use thedocument.linksproperty to access the links by their index, e.g.,document.links[0]to access the first link,document.links[1]to access the second link, etc.
document.head: This property allows you to access the<head>element of the document. You can use this property to add or modify meta tags, scripts and styles.
document.cookie: This property allows you to read and write cookies. You can use this property to set, read, and delete cookies.
document.readyState: This property indicates the current state of the DOM. It can have one of the following values: "loading", "interactive", "complete". This can be useful if you need to perform an action once the page has fully loaded.
document.lastModified: This property returns the date and time that the document was last modified. This can be useful if you want to display the last modified date on a webpage.
document.onloadanddocument.onunload: These events allow you to execute JavaScript code when the page is loading or unloading. You can use theonloadevent to perform initialization tasks, and theonunloadevent to perform cleanup tasks.
Node.cloneNode(): This method creates a duplicate of a node and returns the cloned node. You can use this method to create a copy of an element, which you can then manipulate and insert into the DOM.
Node.replaceChild(): This method replaces a child node with a new node. You can use this method to update an element within the DOM, without having to remove and reinsert the element.
Node.removeChild(): This method removes a child node from the DOM. You can use this method to remove an element from the DOM, or to clear the contents of an element.

