The DOM API
DOM manipulation allows developers to:
- Access elements within an HTML document.
- Create new elements and add them to the document.
- Modify the content, structure, attributes, and styles of HTML elements on a webpage.
- Modify content, attributes, and styles.
- Respond to user interactions (e.g., clicks, keypresses).
- Gets the first element with the specified
id. - Returns an
HTMLElementornullif no match is found.
const element = document.getElementById("elementId");console.log(element); // <div id="elementId">...</div>javascript
- Returns a live
HTMLCollectionof all elements with the specified class name.
const elements = document.getElementsByClassName("myClass");console.log(elements[0]); // First element with class "myClass"javascript
- Returns a live
HTMLCollectionof elements with the specified tag name.
const paragraphs = document.getElementsByTagName("p");console.log(paragraphs.length); // Number of <p> elementsjavascript
- Returns the first element matching a CSS selector.
- Returns
nullif no match is found.
const div = document.querySelector(".myClass");console.log(div); // First element with class "myClass"javascript
- Returns a static
NodeListof all elements matching a CSS selector.
const items = document.querySelectorAll("li.active");items.forEach(item => console.log(item.textContent));javascript
Returns the root node of the document (typically the <html> element).
const root = document.getRootNode();javascript
Finds the closest ancestor (or itself) of an element that matches the given selector.
const element = document.querySelector('.child').closest('.parent');javascript
- Gets or sets the HTML content inside an element.
const div = document.getElementById("myDiv");div.innerHTML = "<p>New content</p>";
// To clear the content of an HTML element.div.innerHTML = ""; // This will remove all child elements and text.javascript
- Gets or sets the text content of an element (ignores HTML tags).
const p = document.querySelector("p");p.textContent = "Plain text only";javascript
- Similar to
textContent, but reflects what’s visible to the user (e.g., respects CSSdisplay: none).
const span = document.querySelector("span");span.innerText = "Visible text";javascript
// Convert HTMLCollection to Arrayconst divArray = Array.from(document.getElementsByTagName('div'));// Orconst divArray2 = [...document.getElementsByTagName('div')];
// Now you can use array methodsdivArray.forEach(div => { // Do something with each div});javascript
A static NodeList is a collection of DOM nodes (elements, text nodes, etc.) that is not live, meaning it does not automatically update when the DOM changes.
A live NodeList is a collection that automatically updates when the DOM structure changes. For example, if a new node is added or removed from the DOM, a live NodeList will reflect those changes immediately.
- Live NodeLists are created by
getElementsBy*methods. - Live NodeLists automatically update when the document changes, while static NodeLists do not.