The code below is supposed to create a list of categories and sub-categories in vanilla js. I am using 2 for-loops. The code works fine for the first level, but not when adding the second level. Can anyone tell me why it only returns the first category? I am a newbie.
function insertAfter(referenceNode, newNode) {
referenceNode.parentNode.insertBefore(newNode, referenceNode.nextSibling);
}
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
var response = JSON.parse(xhttp.responseText);
var allBirds = response.birds;
for (var i = 0; i < allBirds.length; i++) {
var el = document.createElement('li');
el.innerHTML = allBirds[i].family;
var ul = document.getElementById('overview');
insertAfter(ul, el);
var members = allBirds[i].members;
for (var j = 0; j < members.length; j++) {
var elj = document.createElement('h5');
var ulj = document.getElementById('innerView');
insertAfter(elj, ulj);
}
}
}
};
xhttp.open('GET', 'JSON/birds.json', true);
xhttp.send();
This is the desired outcome: category sub-category sub-category category sub-category . .