English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM hasChildNodes() method

HTML DOM Element Object

hasChildNodes()The method returns a boolean value indicating whether the given Node has child nodes.

Note:Whitespace within a node is considered a text node, therefore, if any whitespace or newline characters are retained within an element, the element still has child nodes.

Syntax:

node.hasChildNodes()
var div = document.getElementById("myDiv");
div.hasChildNodes();
Test See </›

Browser compatibility

All browsers fully support the hasChildNodes() method:

Method
hasChildNodes()IsIsIsIsIs

Technical details

Return value:Boolean value, returns true if the node has child nodes, otherwise returns false
DOM Version:DOM Level1

More Examples

If the DIV element has the first child node (index 0), remove the child node:

// Get the DIV element with id="myDiv"
var div = document.getElementById("myDiv");
// If the DIV element has any child nodes, remove its first child node
if (div.hasChildNodes()) {
   div.removeChild(div.childNodes[0]);
}
Test See </›

Related References

HTML DOM Reference:element.childNodes() method

HTML DOM Reference:node.firstChild property

HTML DOM Reference:node.lastChild property

HTML DOM Reference:node.parentNode property

HTML DOM Reference:node.nextSibling property

HTML DOM Reference:node.previousSibling property

HTML DOM Element Object