Nodes are the building blocks of the Document Object Model (DOM) in JavaScript. Understanding nodes is essential for effectively manipulating and traversing the DOM tree. In this chapter, we'll explore nodes in detail, covering their types, properties, methods, and practical usage.
Nodes represent individual elements, attributes, or text content within an HTML document. There are several types of nodes, including element nodes, text nodes, attribute nodes, and more.
Element nodes represent HTML elements, such as <div>
, <p>
, <span>
, etc. These nodes form the backbone of the DOM tree and can have child nodes, including other elements, text nodes, or attribute nodes. Element nodes contain information about the element’s tag name, attributes, and relationships with other nodes.
Hello, world!
var divNode = document.getElementById('myDiv');
console.log(divNode.nodeType); // Output: 1 (Element Node)
divNode
represents the <div>
element with the id myDiv
.nodeType
property, which returns 1
indicating it’s an element node.Text nodes represent the textual content within an element. They contain the actual text characters present between the opening and closing tags of an element. Text nodes are essential for rendering visible content on the web page.
Hello, world!
var textNode = document.getElementById('myDiv').firstChild;
console.log(textNode.nodeType); // Output: 3 (Text Node)
textNode
represents the text content “Hello, world!” within the <div>
element.nodeType
property, which returns 3
indicating it’s a text node.Understanding advanced concepts related to nodes, such as node properties and methods, allows for more sophisticated DOM manipulation.
Nodes have various properties that provide information about their characteristics and relationships within the DOM tree.
nodeType
: Specifies the type of node (e.g., element node, text node, etc.).childNodes
: Returns a NodeList containing all child nodes of the current node.parentNode
: Returns the parent node of the current node.getAttribute()
: Retrieves the value of a specified attribute of the node.
var divNode = document.getElementById('myDiv');
console.log(divNode.childNodes); // Output: NodeList [text]
console.log(divNode.parentNode); // Output: null (because it's the root element)
console.log(divNode.getAttribute('id')); // Output: myDiv
childNodes
property returns a NodeList containing the child nodes of the <div>
element.parentNode
property returns the parent node of the <div>
element.getAttribute()
method retrieves the value of the id
attribute of the <div>
element.createElement()
: Creates a new element node with the specified tag name.createTextNode()
: Creates a new text node with the specified text content.appendChild()
: Appends a child node to the end of the list of children of the specified parent node.
var newNode = document.createElement('p');
var textNode = document.createTextNode('New paragraph');
newNode.appendChild(textNode);
document.body.appendChild(newNode);
createElement()
method creates a new <p>
element.createTextNode()
method creates a text node with the content “New paragraph”.appendChild()
method appends the text node to the paragraph element, and then appends the paragraph element to the body of the document.Nodes are the building blocks of the DOM, and understanding their types, properties, and methods is crucial for effective DOM manipulation in JavaScript. By mastering the concepts of nodes, you can create dynamic and interactive web applications with ease. With practice and experimentation, you'll become proficient in navigating, creating, and modifying nodes within the DOM tree. Happy coding !❤️