Element hasChildNodes Method - Javascript DOM

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.



Return Value:

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:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>

<ul id="myList"><li>Coffee</li><li>Tea</li></ul>

<button onclick="myFunction()">Test</button>

function myFunction() {/* w w  w . j  av a  2  s. com*/
    var list = document.getElementById("myList");

    if (list.hasChildNodes()) {


Related Tutorials