I am new to javascript. For a given 'id' I want to find all it's 1st level child 'id' values. Consider the code snipper below.
function getFirstLevelChildIds(inpId) {
var eleOut = document.getElementById('out');
console.log("NODE");
eleOut.innerHTML += '<br />NODE:'
for(var node in inpId.childNodes) {
console.log("node" + node);
console.log("nodeType" + node.nodeType);
eleOut.innerHTML += '<br />' + "node -> " + node;
}
console.log("CHILD");
eleOut.innerHTML += '<br />CHILD:'
for(var child in inpId.children) {
console.log("child -> " + child);
var eleOut = document.getElementById('out');
eleOut.innerHTML += '<br />' + "child -> " + child;
}
}
var rootID = document.getElementById("A");
getFirstLevelChildIds(rootID);
<div id="A">New York
<div id="B" >Jersey City
<div id="E">Toronto</div>
<div id="F">Boston</div>
</div>
<div id="C">Durango
<div id="G">Atlanta</div>
<div id="H">Milwaukee</div>
<div id="I">Miami</div>
</div>
<div id="D">Greenville
<div id="J">Madison</div>
<div id="K">Washington</div>
</div>
</div>
<div id="out"></div>
Here, the children of A are B,C,D and children of B are E,F
I tried using both childNodes and children calls and didn't work, plus the nodeType is undefined
Can someone please help fix this, using only javascript code (no jQuery)
Thanks, -Sri