You are here: Reference > JavaScript > client-side > HTML DOM > objects > childNodes

childNodes collection

Browser support:
Represents a collection of all nodes that are direct descendants of an element.
The nodes in the collection are sorted in source code order.
The childNodes collection contains all direct descendant nodes including text nodes and comment nodes. If you want to iterate through the child nodes of an element, the firstChild, nextSibling, lastChild and previousSibling properties can also be used.
If you only need the child elements of an element (excluding text nodes and comment nodes), use the nodeType property of the nodes in the childNodes collection or use the children collection.
Note: The children collection also contains the child comment nodes in Internet Explorer before version 9. In other browsers, it only contains the element nodes.

Syntax:

The base interface, through which you can add new functionalities to the childNodes collection, is the NodeList interface.

Possible members:

Properties:
length
Returns an integer that specifies the number of nodes in the current collection.

This property is read-only.
Methods:
[nameOrIndex]
Returns a node or a collection of nodes from the current collection by name or index.

Parameters:

nameOrIndex
Required. String or zero-based integer that specifies the node or nodes to retrieve.
  • If an integer value is specified, it specifies the index of the node to retrieve.
  • If this parameter is a string, then it specifies a value for the name or id property of the element or elements to retrieve.
Internet Explorer and Firefox do not support string values.

Return value:

  • If no match is found, it returns undefined.
  • If exactly one match is found, it returns the matching node.
  • If more than one match is found, it returns a childNodes sub-collection filled with the matching elements. The sub-collection contains the matching elements in source order.
In Google Chrome and Safari, if more than one match is found, the first matching element is returned.
item (nameOrIndex)
Returns an node or a collection of nodes from the current collection by name or index.

Parameters:

nameOrIndex
Required. String or zero-based integer that specifies the node or nodes to retrieve.
  • If an integer value is specified, it specifies the index of the node to retrieve.
  • If this parameter is a string, then it specifies a value for the name or id property of the element or elements to retrieve.
String values are only supported by Opera.

Return value:

  • If no match is found, it returns null in Internet Explorer, Firefox and Opera, and returns undefined in Google Chrome and Safari.
  • If exactly one match is found, it returns the matching node.
  • If more than one match is found, it returns a childNodes sub-collection filled with the matching elements. The sub-collection contains the matching elements in source order.
namedItem (name)
Returns an element or a collection of elements from the current collection by name.

Parameters:

name
Required. String that specifies a value for the name or id property of the element or elements to retrieve.

Return value:

  • If no match is found, it returns null in Internet Explorer, Firefox and Opera, and returns undefined in Google Chrome and Safari.
  • If exactly one match is found, it returns the matching element.
  • If more than one match is found, it returns a childNodes sub-collection filled with the matching elements. The sub-collection contains the matching elements in source order.
tags
Returns a NodeList collection that contains all elements from the current collection with the specified tag name.

Example HTML code 1:

This example illustrates the use of the childNodes collection:
<head>
    <script type="text/javascript">
        function GetChildNodes () {
            var container = document.getElementById ("container");
            for (var i = 0; i < container.childNodes.length; i++) {
                var child = container.childNodes[i];
                if (child.nodeType == 3) {
                    alert ("The " + i + ". child is a text node. Contents:\n" + child.data);
                }
                else {
                    if (child.nodeType == 1) {
                        alert ("The " + i + ". child is an element node. Contents:\n" + child.innerHTML);
                    }
                }
            }
        }
    </script>
</head>
<body>
    <div id="container">
        Some text at the start of the container.
        <button onclick="GetChildNodes ();">Get the child nodes of the container!</button>
        Some text at the end of the container.
    </div>
</body>
Did you find this example helpful? yes no

External links:

User Contributed Comments

Post Content

Post Content