Element.children

« Gecko DOM Reference

Summary

children returns a collection of child elements of the given element.

Syntax and values

var elList = elementNodeReference.children; 

elList is an ordered collection of element objects that are children of the current element. If the element has no children, then elList contains no elements.

The elList is a variable storing the node list of children. Such list is of type HTMLCollection . The children attribute is read-only.

Example

// parg is an object reference to a <p> element
if (parg.childElementCount)
// So, first we check if the object is not empty, if the object has child nodes
 {
   var children = parg.children;
   for (var i = 0; i < children.length; i++) 
   {
   // do something with each child element as children[i]
   // NOTE: List is live, Adding or removing children will change the list
   };
 };

Notes

The items in the collection of elements are objects and not strings. To get data from those node objects, you must use their properties (e.g. elementNodeReference.children[1].nodeName to get the name, etc.).

Browser compatibility

  • Desktop
  • Mobile

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 1 3.5 9 (IE6-8 incl commend nodes) 10 4
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support (Yes) (Yes) (Yes) (Yes) (Yes)

 

Tags (3)

Edit tags

Attachments (0)

 

Attach file