This page is Almost Ready

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

hasChildNodes

Summary

Gets a value that indicates whether the Node has any direct Node descendant of any type.

Method of dom/Nodedom/Node

Syntax

var hasChildNodes = element.hasChildNodes();

Return Value

Returns an object of type BooleanBoolean

Whether or not the Node contains any direct Node descendant of any type

Examples

The following example removes the first child node inside the element with the id “foo” if foo has child nodes.

var foo = document.getElementById("foo");

if (foo.hasChildNodes()) {
  foo.removeChild(foo.childNodes[0]);
}

{{Notes_Section |Notes=If the Node contains any [[dom/Element|Element], [dom/Text|Text] or other type of nodes, they can be accessed from the childNodes collection. }}

Related specifications

Document Object Model (DOM) Level 3 Core
W3C Recommendation

Attributions