Element removeChild Method - Remove a <li> element from its parent, and insert it again: - Javascript DOM

Javascript examples for DOM:Element removeChild

Description

Element removeChild Method - Remove a <li> element from its parent, and insert it again:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<ul id="myList">
  <li id="myLI">Coffee</li>
</ul>/*ww w .  j  av  a2s.  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>

Related Tutorials