Element removeChild Method - Find out if a list has any child nodes. If so, remove its first child node (index 0): - Javascript DOM

Javascript examples for DOM:Element removeChild

Description

Element removeChild Method - Find out if a list has any child nodes. If so, remove its first child node (index 0):

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<ul id="myList"><li>Coffee</li><li>Tea</li><li>Milk</li></ul>

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

<script>
function myFunction() {/*from w  w w.  ja v  a  2  s . c  om*/
    var list = document.getElementById("myList");

    if (list.hasChildNodes()) {
        list.removeChild(list.childNodes[0]);
    }
}
</script>

</body>
</html>

Related Tutorials