Element hasChildNodes Method - Javascript DOM

Javascript examples for DOM:Element hasChildNodes

Description

The hasChildNodes() method returns true if the node has any child nodes, otherwise false.

Whitespace is considered as text nodes.

Parameters

None

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>
<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>

Related Tutorials