DOM Node hasChildNodes() Method
Example
Find out if an <ul> element has any child nodes:
var list = document.getElementById("myList").hasChildNodes();
The result of list will be:
true
Try it Yourself »
More "Try it Yourself" examples below.
Definition and Usage
The hasChildNodes() method returns true if the specified node has any child nodes, otherwise false.
Note: Whitespace inside a node is considered as text nodes, so if you leave any white space or line feeds inside an element, that element still has child nodes.
Browser Support
Method | |||||
---|---|---|---|---|---|
hasChildNodes() | Yes | Yes | Yes | Yes | Yes |
Syntax
node.hasChildNodes()
Parameters
None |
Technical Details
Return Value: | A Boolean, returns true if the node has child nodes, false otherwise |
---|---|
DOM Version | Core Level 1 Node Object |
More Examples
Example
Remove the first child node (index 0) inside an <ul> element, if the element has any child nodes:
// Get the <ul> element with id="myList"
var list = document.getElementById("myList");
// If the <ul> element has
any child nodes, remove its first child node
if (list.hasChildNodes()) {
list.removeChild(list.childNodes[0]);
}
Try it Yourself »
Related Pages
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