DOM - Child Nodes of a Node

> (World Wide) Web - (W3|WWW) > The Document Object Model W3C API Interface (DOM)

1 - About

Child Nodes of an element in the dom tree

Advertising

3 - Management

3.1 - Get

The childNodes property of an element returns a collection of child Node.

var divChildNodes  = document.querySelector("div").childNodes;
 
console.log("We have " + divChildNodes.length + " nodes");
 
for (var i = 0; i < divChildNodes.length; i++) {
        console.log("Node ("+i+") is a "+divChildNodes[i]+" and has the following content: "+divChildNodes[i].textContent);
}
<div>
<p>First paragraph</p>
<p>Second paragraph</p>
<b>
</div>

3.2 - Add

3.2.1 - Web API Add

Browser - DOM Web Api

for(var i = 1; i <= 4; i++) {
        var myDiv = document.createElement('div');
        myDiv.className = "buttonClass";
        myDiv.innerHTML = "Div content "+i;
        document.body.appendChild(myDiv);
}

3.2.2 - Jquery