HTML elements are taken from the container. If the parent node has a child, make a button and insert child.id from child in the button. Everything works in the code, but does not want appendChild (h2);
It should look like:
<button id = "parent2"> <h2> child1 </h2> <h2> child2 </h2> </button>
<div id="container">
<div id="parent1"></div>
<div id="parent2">
<div id="child1"></div>
<div id="child2"></div>
<div id="child3"></div>
</div>
</div>
<p id="demo"></p>
var parent = document.getElementById("container").querySelectorAll("*");
for (let i = 0; i < parent.length; i++) {
if(parent[i].hasChildNodes()){
var btn = document.createElement("BUTTON");
btn.id = parent[i].id;
document.getElementById("demo").appendChild(btn);
}
let children = parent[i].childNodes;
for (let i = 0; i < children.length; i++) {
if(children[i]){
var h2 = document.createElement("H2");
h2.innerHTML = children[i].id;
parent[i].appendChild(h2);
}else{}
}
}