Element removeChild Method - Remove all child nodes of a list: - Javascript DOM

Javascript examples for DOM:Element removeChild

Description

Element removeChild Method - Remove all child nodes of a list:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

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

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

<script>
function myFunction() {/*from  w  ww .j  a va  2s  .c  o m*/
    var list = document.getElementById("myList");
    while (list.hasChildNodes()) {
        list.removeChild(list.firstChild);
    }
}
</script>

</body>
</html>

Related Tutorials