Javascript DOM HTML Node removeChild Method remove and add back

Introduction

Remove a <li> element from its parent, and insert it again:

Click one button to remove the li element from ul.

Click the other button to insert the removed li element to ul.

View in separate window

<!DOCTYPE html>
<html>
<body>

<ul id="myList">
  <li id="myLI">Coffee</li>
</ul>//  w  w w  . j  a v  a 2  s.  c o m
<button onclick="removeLi()">Remove li</button>
<button onclick="appendLi()">Insert li</button>

<script>
var item = document.getElementById("myLI");

function removeLi() {
  item.parentNode.removeChild(item);
}

function appendLi() {
  var list = document.getElementById("myList");
  list.appendChild(item);
}
</script>

</body>
</html>



PreviousNext

Related