Robert Kusznier
Robert Kusznier

Reputation: 6911

Is there a difference between children[0] and firstElementChild?

ParentNode interface declares property firstElementChild. I have seen people using children[0] everywhere, so it makes me wonder, is there a difference between using firstElementChild and children[0]?

Upvotes: 2

Views: 2535

Answers (1)

Jonathan Brizio
Jonathan Brizio

Reputation: 1210

children[0] will produce the same result as firstElementChild. Will firstElementChild returns the first child node as an element node (ignores text and comment nodes). The children property is fully supported in IE6 to IE8. However, in these versions, it returns element nodes AND comment nodes. IE9+ returns only element nodes.

Source: W3C

Keep rocking!

Upvotes: 2

Related Questions