Table of contents
- 1. Summary
- 2. Syntax and values
- 3. Example
- 4. Notes
- 5. See also
- 6. Browser compatibility
- 7. Specification
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.).
See also
Browser compatibility
| 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) |
Mozilla Developer Network