The DOM Level 2 and level 3
The DOM Level 2 and level 3
DOM Changes
The DOM (Document Object Model) represents a webpage as a tree of nodes (elements, text,
etc.). "DOM Changes" refers to modifying this tree dynamically using JavaScript.
Adding Elements:
o Use createElement() to make a new element, then attach it with
appendChild(), prepend(), or insertBefore().
document.body.insertBefore(newSpan, existingDiv);
Removing Elements:
o Use removeChild() (older) or remove() (modern).
// Modern way
anotherP.remove();
Replacing Elements:
o Use replaceChild() to swap one node for another.
document.body.replaceChild(newDiv, oldDiv);
2. XML Namespaces
XML Namespaces prevent naming conflicts in XML-based documents (e.g., XHTML, SVG)
by associating elements/attributes with a unique identifier (namespace URI).
Syntax in XML:
<svg xmlns="http://www.w3.org/2000/svg">
</svg>
In JavaScript:
o Use createElementNS() to create elements with a namespace.
o Use getElementsByTagNameNS() or setAttributeNS() for namespace-aware
operations.
svg.appendChild(rect);
document.body.appendChild(svg);
wrapped in a div
3. Other Changes
Changing Attributes:
img.setAttribute("src", "example.jpg");
document.body.appendChild(img);
// Or directly:
img.src = "newimage.jpg";
Modifying Text:
let p = document.createElement("p");
document.body.appendChild(p);
Inner HTML:
document.body.appendChild(div);
Classes:
span.classList.add("highlight", "bold");
document.body.appendChild(span);
4. Styles
div.style.backgroundColor = "lightblue";
div.style.padding = "10px";
div.style.fontFamily = "Arial";
document.body.appendChild(div);
Multiple Styles:
let p = document.createElement("p");
Object.assign(p.style, {
color: "green",
fontSize: "20px",
});
document.body.appendChild(p);
Inline Styles:
div.style.color = "red";
console.log(div.style.color); // "red"
document.body.appendChild(div);
Computed Styles:
o Use getComputedStyle() for all applied styles (inline + stylesheets).
console.log(styles.fontSize); // "16px"
console.log(styles.getPropertyValue("font-size")); // "16px"
Accessing Stylesheets:
Adding Rules:
document.head.appendChild(style);
Modifying Rules:
sheet.cssRules[0].style.fontSize = "30px";
Removing Rules:
sheet.deleteRule(0);
7. Element Dimensions
Basic Dimensions:
o offsetWidth/offsetHeight: Includes borders.
o clientWidth/clientHeight: Excludes borders, includes padding.
div.style.width = "100px";
div.style.height = "100px";
document.body.appendChild(div);
console.log(div.clientWidth); // 100
Position:
o offsetTop/offsetLeft: Relative to offset parent.
o getBoundingClientRect(): Relative to viewport.
8. Traversals
Basic Traversal:
// HTML: <div><p>Text</p><span>Span</span></div>
console.log(div.parentNode); // <body>
let p = div.querySelector("p");
9. NodeIterator
Basic Usage:
// HTML: <body><p>One</p><span>Two</span></body>
document.body,
);
let node;
With Filter:
let filter = {
};
10. TreeWalker
Basic Usage:
// HTML: <body><div><p>One</p><span>Two</span></div></body>
console.log(walker.nextNode().tagName); // "DIV"
console.log(walker.nextNode().tagName); // "P"
console.log(walker.nextNode().tagName); // "SPAN"
console.log(walker.previousNode().tagName); // "P"
With Filter:
console.log(walker.nextNode().textContent); // "Two"
11. Ranges
let p = document.querySelector("p");
Manipulating:
range.insertNode(document.createTextNode("New content"));
Partial Selection:
range.setStart(textNode, 0);
range.setEnd(textNode, 5);
console.log(range.toString()); // "Hello"
Advanced Usage:
// HTML: <body><div><p>First</p><p>Second</p></div></body>
document.body.appendChild(fragment);
// Result: <body><div></div><p>First</p><p>Second</p></body>
Cloning:
document.body.appendChild(clone);
Basic Usage:
textRange.moveToElementText(document.querySelector("p"));
textRange.select(); // Highlights it
Positioning:
Note: TextRange is obsolete. Modern browsers (including Edge) use the standard Range API,
so focus on that unless supporting IE < 11.