Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Dom Nodes in JavaScript

Dom Nodes in JavaScript

? DOM Nodes in JavaScript


What is a DOM Node?

  • A Node is the basic building block of the DOM tree.

  • Everything in the DOM is a node: elements, text, comments, attributes, etc.

  • Nodes are objects representing parts of the document.


Types of DOM Nodes

Node TypeDescriptionNodeType Value (Number)
Element NodeRepresents HTML elements (e.g., <div>)1
Attribute NodeRepresents attributes (e.g., class)2
Text NodeRepresents text inside elements3
Comment NodeRepresents comments (<!-- comment -->)8
Document NodeRepresents the entire document9

Common Node Properties

PropertyDescription
nodeTypeNumeric code identifying the node type
nodeNameName of the node (tag name for elements)
nodeValueValue of the node (text for text nodes)
parentNodeReference to the parent node
childNodesCollection of child nodes
firstChildFirst child node
lastChildLast child node
nextSiblingNext sibling node
previousSiblingPrevious sibling node

Example: Inspecting Node Types

<div id="myDiv">Hello <span>World</span><!-- comment --></div><script>const div = document.getElementById("myDiv");// Element nodeconsole.log(div.nodeType);      // 1console.log(div.nodeName);      // "DIV"// Text node (first child)const textNode = div.firstChild;console.log(textNode.nodeType); // 3console.log(textNode.nodeValue); // "Hello "// Comment node (last child)const commentNode = div.lastChild;console.log(commentNode.nodeType); // 8console.log(commentNode.nodeValue); // " comment "</script>

Manipulating Nodes

  • You can create nodes with document.createElement(), document.createTextNode().

  • Nodes can be inserted or removed using DOM methods like appendChild(), removeChild().

const newText = document.createTextNode("New text node");div.appendChild(newText);

Summary

  • DOM Nodes represent every part of the document.

  • Key types: element nodes, text nodes, comment nodes, etc.

  • Understanding nodes helps in fine-grained manipulation of HTML.


Want to learn how to create and insert different node types dynamically or traverse nodes recursively?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql