Javascript DOM HTML UL remove all child nodes

Description

Javascript DOM HTML UL remove all child nodes

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() {/*  w  w  w.  j av a 2  s  .  c om*/
  var list = document.getElementById("myList");
  while (list.hasChildNodes()) {
    list.removeChild(list.firstChild);
  }
}
</script>

</body>
</html>



PreviousNext

Related