VOOZH about

URL: https://www.geeksforgeeks.org/html/html-dom-childnodes-property/

⇱ HTML DOM childNodes Property - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

HTML DOM childNodes Property

Last Updated : 13 Jun, 2023

The childNodes property returns a node's child nodes as a nodeList object. White spaces and comments are also considered as nodes. The nodes are assigned index numbers, starting from 0. Searching and sorting operations can be performed using index number on the node list. Syntax:

elementNodeReference.childNodes;

Return Value: It returns a collection of child nodes of a particular node as a nodeList object (including white spaces, texts, and comments, which are considered as nodes). Properties:

  1. length property: It determines the number of child nodes of the object. It is a read only property. Syntax:
elementNodeReference.childNodes.length;
elementNodeReference.childNodes[index_number].length;
  1. Example-1: Showing length property. 
  1. Output: Before clicking on the button: 👁 Image
    After clicking on the button: 👁 Image
  2. nodeName property: It returns the name of the specified node. If the node is an element node, it will return the tag name else if the node is an attribute node, it will return the attribute name else for different node types, different names will be returned. Syntax:
elementNodeReference.childNodes[index_number].nodeName;
  1. Example-2: Showing nodeName property 
  1. Output: Before clicking on the button: 👁 Image
    After clicking on the button: 👁 Image
  2. nodeValue property: It sets or returns the node value of the specified node. Syntax:
elementNodeReference.childNodes[index_number].nodeValue;
  1. Example-3: Showing nodeValue property 
  1. Output: Before clicking on the button:
  2.  👁 Image
    After clicking on the button: 👁 Image

Browser Support: The listed browsers support DOM childNodes property:

  • Google Chrome 1+
  • Edge 12+
  • Firefox 1+
  • Internet Explorer 5+
  • Opera 7+
  • Safari 1.2+
Comment