How to use JavaScript Dom Node hasChildNodes

In this article I have described about Dom Node hasChildNodes method used in JavaScript.
  • 1488

JavaScript Dom Node hasChildNodes() method

  • As we already know that node object is used to represent and add node within the HTML document.
  • The hasChildNodes() method checks whether any node has any child node or not and return boolean value.
  • It returns true if there is any child node otherwise false.

Browser support

Following are the main browsers which support the hasChildNodes() method of Dom Node object

  • Internet Explorer.
  • Firefox.
  • Opera.
  • Google Chrome.
  • Safari.

Syntax

The hasChildNodes() of Dom Node object has following syntax

node.hasChildNodes()

Lets take an example

<!DOCTYPE html>

<html>

<body style ="background-color:yellow">

<ul id="myList"><li>AMAN</li><li>NITIN</li></ul>

<p id="demo">To check element has any child nodes</p>

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

<script type="text/javascript">

    function myFunction() {

        var lst = document.getElementById("myList");

        var x = document.getElementById("demo");

        x.innerHTML = lst.hasChildNodes();

    }

</script>

</body>

</html>

 

Output


  fig1.jpg


When we click on button


fig2.jpg

Further Readings

You may also want to read these related articles :here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.