How to use JavaScript Dom Node hasChildNodes

In this article I have described about Dom Node hasChildNodes() method in JavaScript.
  • 2282

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>

<p id="menu">Display Color</p>

<button onclick="myFunction()">Try it</button>

<script type="text/javascript">

    Boolean.prototype.Blcolor = function () {

        if (this.valueOf() == true) {

            this.color = "YELLOW";

        }

        else

      {

            this.color = "RED";

        }

    }

 

    function myFunction() {

        var a = new Boolean(1);

        a.Blcolor();

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

        x.innerHTML = a.color;

    }

</script>

</body>

</html>

 

Output


dn child1.jpg


When we click on button


dn child2.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

© 2019 DotNetHeaven. All rights reserved.