The above code will be understood and executed by Node.js to print "Hello, Node.js!" in the console.
Node in the Document Object Model (DOM)
In the DOM, everything is a node: the document itself, elements, text inside elements, and comments. This is often visualized as a tree structure, where the document is the root node and all other elements are its children (or child nodes).
In the above HTML document, the
<p> elements are all nodes. The text inside
<p> is also considered as nodes (text nodes).
How to Manipulate DOM Nodes
// Change the text within a node document.getElementById("title").textContent = "Hello, DOM Nodes!"; // Add a new node let newNode = document.createElement("p"); newNode.textContent = "This is a new node."; document.body.appendChild(newNode); // Delete a node let nodeToDelete = document.getElementById("text"); nodeToDelete.parentNode.removeChild(nodeToDelete);
In the above code, we first change the text in the
<h1> element with id "title". Then, we create a new
<p> node, set its text, and append it to the
<body> node. Finally, we select the
<p> node with id "text" and remove it from the DOM.