X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

DOM Node hasChildNodes() Method

Element Object Reference Element Object

Example

Find out if an <ul> element has any child nodes:

var list = document.getElementById("myList").hasChildNodes();                   

The result of list will be:

true

Try it yourself »

More "Try it Yourself" examples below.


Definition and Usage

The hasChildNodes() method returns true if the specified node has any child nodes, otherwise false.

Note: Whitespace inside a node is considered as text nodes, so if you leave any white space or line feeds inside an element, that element still has child nodes.


Browser Support

Method
hasChildNodes() Yes Yes Yes Yes Yes


Syntax

node.hasChildNodes()

Parameters

None

Technical Details

Return Value: A Boolean, returns true if the node has child nodes, false otherwise
DOM Version Core Level 1 Node Object


Examples

More Examples

Example

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

// Get the <ul> element with id="myList"
var list = document.getElementById("myList");

// If the <ul> element has any child nodes, remove its first child node
if (list.hasChildNodes()) {
    list.removeChild(list.childNodes[0]);
}

Try it yourself »


Related Pages

HTML DOM reference: element.childNodes() Method

HTML DOM reference: node.firstChild Property

HTML DOM reference: node.lastChild Property

HTML DOM reference: node.parentNode Property

HTML DOM reference: node.nextSibling Property

HTML DOM reference: node.previousSibling Property


Element Object Reference Element Object


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook