{{ APIRef("DOM") }}
The ParentNode.children
read-only property returns a live {{domxref("HTMLCollection")}} of child elements of the given object.
The items in the returned collection 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.).
Syntax
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 {{ domxref("HTMLCollection") }}. The children
attribute is read-only.
Example
// pEl is an object reference to a <p> element // So, first we check if the object is not empty, if the object has child elements if (pEl.children.length) { var children = pEl.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 } }
Specification
Specification | Status | Comment |
---|---|---|
{{SpecName('DOM WHATWG', '#dom-parentnode-children', 'ParentNode.children')}} | {{Spec2('DOM WHATWG')}} | Initial definition. |
Browser compatibility
{{ CompatibilityTable() }}
Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari |
---|---|---|---|---|---|
Basic support (on {{domxref("Element")}}) | 1.0 | {{CompatGeckoDesktop("1.9.1")}} | 9.0 [1] | 10.0 | 4.0 |
Support on {{domxref("Document")}} and {{domxref("DocumentFragment")}} {{experimental_inline}} | 29.0 | {{CompatGeckoDesktop("25.0")}} | {{CompatNo}} | 16.0 | {{CompatNo}} |
Support on {{domxref("SVGElement")}} | {{CompatVersionUnknown}} | {{CompatVersionUnknown}} | {{CompatNo}} | {{CompatUnknown}} | {{CompatNo}} |
Feature | Android | Firefox Mobile (Gecko) | IE Mobile | Opera Mobile | Safari Mobile |
---|---|---|---|---|---|
Basic support (on {{domxref("Element")}}) | {{ CompatVersionUnknown() }} | {{CompatGeckoMobile("1.9.1")}} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} | {{ CompatVersionUnknown() }} |
Support on {{domxref("Document")}} and {{domxref("DocumentFragment")}} {{experimental_inline}} | {{CompatVersionUnknown}} | {{CompatGeckoMobile("25.0")}} | {{CompatNo}} | 16.0 | {{CompatNo}} |
[1] Internet Explorer 6, 7 and 8 supported it, but erroneously includes {{domxref("Comment")}} nodes.
See also
- The {{domxref("ParentNode")}} and {{domxref("ChildNode")}} pure interfaces.
-
Object types implementing this pure interface: {{domxref("Document")}}, {{domxref("Element")}}, and {{domxref("DocumentFragment")}}.