Javascript DOM HTML Node removeChild Method remove all child nodes

Introduction

Remove all child nodes of a list:

Click the button to remove all child nodes of ul.

View in separate window

<!DOCTYPE html>
<html>
<body>

<ul id="myList"><li>CSS</li><li>HTML</li><li>Java</li></ul>
<button onclick="myFunction()">Test</button>

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

</body>
</html>



PreviousNext

Related