DHTML Collections
DOM Level 1 Specification.
Retrieves a collection of the child nodes of the specified object.
[ oNodeList = ] object.childNodes |
[ oNode = ] object.childNodes ( vIndex [ , iSubIndex ] ) |
oNodeList |
Reference to an array of elements contained by the object. |
oCellObject |
Reference to an individual item in the array of elements contained by the object. |
vIndex |
Required. Number or string specifying the element or collection to retrieve. If this parameter is a number, the method returns the element in the collection at the given position, where the first element has value 0, the second has 1, and so on. If this parameter is a string and there is more than one element with the name or id property equal to the string, the method returns a collection of matching elements. |
iSubIndex |
Optional. Position of an element to retrieve. This parameter is used when vIndex is a string. The method uses the string to construct a collection of all elements that have a name or id equal to the string and then retrieves from this collection the element at the position specified by iSubIndex. |
If there are no children, this is a nodeList containing no nodes.
The content of the returned nodeList is "live" in the sense that, for instance, changes to the children of the node object that it was created from are immediately reflected in the nodes returned by the nodeList accessors; it is not a static snapshot of the content of the node. This is true for every nodeList, including the ones returned by the getElementsByTagName method.
The following samples demonstrate implementation of the childNodes collection.
The first sample shows how the childNodes collection of this document's BODY object is assigned to a variable, and displayed on call.
function getNodes ( ) {
// assign childNodes of BODY to variable
var nodeList = document.body.childNodes;
// loop thru the nodeList to retrieve each nodeName
var els = "";
for ( i = 0; i < nodeList.length; i ++ )
els += nodeList [ i ].nodeName + "\n";
// display list
alert ( els );
}
Show me
The second sample shows how the childNodes collection of a node created with the createElement method is assigned to a variable.
var oParentNode = document.createElement ( "DIV" );
var oNode = document.createElement ( "B" );
document.body.insertBefore ( oParentNode );
oParentNode.insertBefore ( oNode );
var aNodeList = oParentNode.childNodes;
parentNode