Element hasChildNodes Method - Remove the first child node (index 0) inside an <ul> element, if the element has any child nodes: - Javascript DOM

Javascript examples for DOM:Element hasChildNodes

Description

Element hasChildNodes Method - Remove the first child node (index 0) inside an <ul> element, if the element has any child nodes:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

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

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

<script>
function myFunction() {//w  ww  .ja  va 2  s  . c  o m
    var list = document.getElementById("myList");

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

</body>
</html>

Related Tutorials