HTML | DOM hasChildNodes() Method
The HTML hasChildNodes() property will return true if the given node has a child node or false if it doesn’t have any child nodes. A blank line or whitespace is also treated a child node so it also returns true on a blank line or whitespace.
Prerequist DOM (Document Object Model)
Parameters:
No parameters required.
Return value: The HTML nodeValue property returns the following type of values.
Syntax
node.hasChildNodes()
Example-1: In the following paragraph tag has nothing so its return false
<!DOCTYPE html> <html> <body> <head> <script> // utility function to demonstarte hasChildNode Property function exampleFunction() { // var x used to get the information of those node // for which you want to perform // hasChildNode properties var res = document.getElementById("divId").hasChildNodes(); document.getElementById("GeeksForGeeks"+ ).innerHTML = res; } </script> </head> <!--In this example we will create a node of the type div and a button which calls a function name "exampleFunction" on clicking of this button it will show the properties of hasChildNode property --> <p id="divId"></p> <br> <button onclick="exampleFunction()"> click to know the paragraph tag has any child node </button> <!-- below paragraph Tag is used to print the value of nodeValue properties--> <p id="GeeksForGeeks"></p> </body> </html> |
Output:

Example-2 :In the following paragraph tag has a massage so its return true
<!DOCTYPE html> <html> <body> <head> <script> // utility function to demonstarte hasChildNode Property function exampleFunction() { // var x used to get the information of those nodes // for which you want to perform // hasChildNode properties. var res = document.getElementById("divId").hasChildNodes(); document.getElementById("GeeksForGeeks"+ ).innerHTML = res; } </script> </head> <!--In this example we will create a node of the type div and a button which calls a function name "exampleFunction" on clicking of this button it will show the properties of hasChildNode property --> <p id="divId">Hello geeksforgeeks</p> <br> <button onclick="exampleFunction()"> click to know the paragraph tag has any child </button> <!-- below paragraph Tag is used to print the value of nodeValue properties--> <p id="GeeksForGeeks"></p> </body> </html> |
Output:

Supported Browsers: The browser supported by DOM click() Method are listed below:
Recommended Posts:
- HTML | DOM contains() Method
- HTML | DOM isEqualNode() Method
- HTML | DOM blur() Method
- HTML | canvas arc() Method
- HTML | DOM createDocumentFragment() Method
- HTML | DOM console.log() Method
- HTML | DOM History go() Method
- HTML | DOM appendChild() Method
- HTML | DOM getBoundingClientRect() Method
- HTML | DOM hasFocus() Method
- HTML | DOM setAttributeNode() Method
- HTML | DOM write() Method
- HTML | DOM replaceChild() Method
- HTML | DOM cloneNode() Method
- HTML | DOM querySelectorAll() Method
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



