W3C DOM tests - childNodes and children

This is element x. It has a B and an I and a span as its children.

This <div> is element y and contains three paragraphs and a comment (which is of course invisible).

Paragraph 1

Paragraph 2

Paragraph 3

Above is an empty <div> that is element z. It has no child nodes.

This page tests the children[] and childNodes[] node lists.

Test scripts

childNodes[]

Test the number of childNodes. Correct answer: 9

alert(x.childNodes.length)

Get the name of the second child node. Correct answer: CODE

alert(x.childNodes[1].nodeName)

Test the number of childNodes with empty text nodes messing things up. Correct answer: either 13 (empty text nodes counted) or 9 (empty text nodes not counted)

alert(y.childNodes.length)

children[]

Test the number of children of x. Correct answer: 4

alert(x.children.length)

Test the number of children of y. Correct answer: 5

alert(y.children.length)

Get the name of the second element child node. Correct answer: B

alert(x.children[1].nodeName)

hasChildNodes()

Does element x have child nodes? Correct answer: true

alert(x.hasChildNodes())

Does element z have child nodes? Correct answer: false

alert(z.hasChildNodes())