Nodes & Navigation¶
According to the W3C HTML DOM standard, everything in an HTML document is a node: - The entire document is a document node. - Every HTML element is an element node. - The text inside HTML elements are text nodes. - All comments are comment nodes.
Navigating Between Nodes¶
You can use the following node properties to navigate between nodes with JavaScript:
parentNodechildNodes[n]firstChild/lastChildnextSibling/previousSibling
Creating New Elements¶
To add a new element to the HTML DOM, you must create the element (element node) first, and then append it to an existing element.
// 1. Create element
const para = document.createElement("p");
const node = document.createTextNode("This is new.");
para.appendChild(node);
// 2. Find parent
const element = document.getElementById("div1");
// 3. Append
element.appendChild(para);
Removing Elements¶
Modern Methods
Modern JS has cleaner methods for manipulation: append(), prepend(), before(), after().