Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

HTML DOM children property

HTML DOM children property

Element Object Reference Element object

Example

Get the collection of child elements of the body element:

var

c

=

document

.

body

.

children

;

Try it »


Definition and Usage

The children property returns a collection of the element's child elements, which is an HTMLCollection object.

Tip: Sorted by the order in which child elements appear in the element. Use the length property of the HTMLCollection object to get the number of child elements, then use the index number (starting from 0) to access each child element.

The difference between the children property and the childNodes property:


Browser support

property
children 2.0 9.0* 3.5 4.0 10.0

Note: IE6 to IE8 fully support the children property, but they return both element nodes and comment nodes; IE9 and later versions only return element nodes.


Syntax

element.children

Technical Details

Return Value: An HTMLCollection object representing a collection of element nodes, where the order of elements in the collection is their order in the source code.
DOM version DOM 1

More examples

Example

Check how many child elements the div has:

var

c

=

document

.

getElementById

(

"

myDIV

"

)

.

children

.

length

;

Try it »

Example

Modify the background color of the second child element of the div element:

var

c

=

document

.

getElementById

(

"

myDIV

"

)

.

children

;

c

[

1

]

.

style

.

backgroundColor

=

"

yellow

"

;

Try it »

Example

Get the text of the third child element (index 2) of the select element:

var

c

=

document

.

getElementById

(

"

mySelect

"

)

.

children

;

document

.

getElementById

(

"

demo

"

)

.

innerHTML

=

c

[

2

]

.

text

;

Try it »

Example

Modify the background color of all child elements of the body element:

var

c

=

document

.

body

.

children

;

var

i

;

for

(

i

=

0

;

i

<

c

.

length

;

i

++

)

{

c

[

i

]

.

style

.

backgroundColor

=

"

red

"

;

}

Try it »


Element Object Reference Element object