Javascript examples for DOM:Element hasChildNodes
The hasChildNodes() method returns true if the node has any child nodes, otherwise false.
Whitespace is considered as text nodes.
None
A Boolean, returns true if the node has child nodes, false otherwise
The following code shows how to Find out if an <ul> element has any child nodes:
<!DOCTYPE html> <html> <body> <ul id="myList"><li>Coffee</li><li>Tea</li></ul> <button onclick="myFunction()">Test</button> <script> function myFunction() {/* w w w . j av a 2 s. com*/ var list = document.getElementById("myList"); if (list.hasChildNodes()) { list.removeChild(list.childNodes[0]); } } </script> </body> </html>