DOM node children's collections - what's the difference?

What is the difference between children and childNodes from node? And childElementCount and childNodes.length?

+6
javascript dom nodes
source share
2 answers

element.childNodes and element.childNodes.length : includes child elements, text nodes (including node nodes between elements) and comments (plus potentially CDATASections, ProcessingInstructions, DocumentTypes and EntityReferences, depending on the document and parser). Defined by the DOM Level 1 Core standard and has been available in every browser for many years.

element.children and element.childElementCount : contains only the nodes of the elements, so in some cases they are easier to handle. childElementCount defined by the Element Traversal API standard and is available in the latest versions of Mozilla, Opera, and WebKit. children not defined by any standard, but is available in IE and updated versions of Mozilla, Opera, WebKit, and IE.

+19
source share

Children contain only items. ChildNodes contains all nodes.

+2
source share

All Articles