English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
node.hasChildNodes()
var div = document.getElementById("myDiv"); div.hasChildNodes();Test See </›
All browsers fully support the hasChildNodes() method:
Method | |||||
hasChildNodes() | Is | Is | Is | Is | Is |
Return value: | Boolean value, returns true if the node has child nodes, otherwise returns false |
---|---|
DOM Version: | DOM Level1 |
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 </›
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