Node Properties:Type, Tag and Contents

JavaScript Node Properties: Type, Tag, and Contents 

In the Document Object Model (DOM), everything in an HTML page is a node (elements, text, attributes, comments, etc.). JavaScript allows us to access and modify these nodes using various node properties.

1️⃣ Node Type (nodeType)

Every node in the DOM has a numeric type value that identifies what kind of node it is.

📌 Common nodeType Values

Node TypeValueExample
Element Node1<div>, <p>
Text Node3"Hello" inside <p>Hello</p>
Comment Node8<!-- This is a comment -->
Document Node9The document itself

📌 Example: Get Node Type

let element = document.getElementById("demo"); console.log(element.nodeType); // 1 (Element Node) let textNode = element.firstChild; console.log(textNode.nodeType); // 3 (Text Node)

🔹 element.firstChild gets the first child, which is usually a Text Node (even whitespace).

2️⃣ Node Tag Name (nodeName and tagName)

  • nodeName returns the name of the node (uppercase for elements).
  • tagName returns the element’s tag name (only for elements).

📌 Example: Get Tag Name

let div = document.getElementById("demo"); console.log(div.nodeName); // "DIV" console.log(div.tagName); // "DIV"

🔹 For non-element nodes, nodeName returns different values:

  • Text node → #text
  • Comment node → #comment
  • Document node → #document

3️⃣ Node Content (innerHTML, textContent, innerText)

📌 innerHTML (HTML Content)

  • Gets/sets HTML inside an element.
  • Includes all HTML tags.
document.getElementById("demo").innerHTML = "<b>Bold Text</b>"; console.log(document.getElementById("demo").innerHTML);

📌 textContent (Pure Text)

  • Gets/sets only text (ignores HTML).
  • Includes hidden elements' text.
console.log(document.getElementById("demo").textContent);

📌 innerText (Visible Text)

  • Gets/sets only visible text.
  • Ignores hidden text (CSS display: none).
console.log(document.getElementById("demo").innerText);

4️⃣ Full Example

<div id="demo">Hello <b>World</b>!</div> <script> let div = document.getElementById("demo"); console.log("Node Type:", div.nodeType); // 1 (Element Node) console.log("Tag Name:", div.tagName); // "DIV" console.log("Inner HTML:", div.innerHTML); // "Hello <b>World</b>!" console.log("Text Content:", div.textContent); // "Hello World!" console.log("Inner Text:", div.innerText); // "Hello World!" </script>

🚀 Summary

PropertyDescription
nodeTypeReturns a numeric value representing the type of node.
nodeNameReturns the name of the node (e.g., "DIV", "#text").
tagNameReturns the tag name of an element (only for elements).
innerHTMLGets/sets the HTML inside an element.
textContentGets/sets only text (ignores hidden text).
innerTextGets/sets only visible text (ignores hidden elements).

🚀 Now you can manipulate and access DOM nodes easily! 🎯
Need more details? Let me know! 🚀

Soeng Souy

Soeng Souy

Website that learns and reads, PHP, Framework Laravel, How to and download Admin template sample source code free.

Post a Comment

CAN FEEDBACK
Ad