Element insertBefore Method - Move a <li> element from one list to another: - Javascript DOM

Javascript examples for DOM:Element insertBefore

Description

Element insertBefore Method - Move a <li> element from one list to another:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<ul id="myList1"><li>Coffee</li><li>Tea</li></ul>

<ul id="myList2"><li>Water</li><li>Milk</li></ul>

<p>Click the button to move an item from one list to another</p>

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

<script>
function myFunction() {//from   w  w  w . ja v  a 2s.c  o  m
    var node = document.getElementById("myList2").lastChild;
    var list = document.getElementById("myList1");
    list.insertBefore(node, list.childNodes[0]);
}
</script>

</body>
</html>

Related Tutorials