I have the website written in 'content.json' file, so my html.index has an empty body. Currently I can console.log all the elements in 'script.js' and I can see in console all the divs that I declared in 'content.json'. My question is how to append all those elements to html body so I can actually see all my divs that I've written in json?
I used recursion to get all those elements from .json and console.log() them. This is my json file (just in case). https://textuploader.com/15820
function loadJSON(callback) {
var req = new XMLHttpRequest();
req.overrideMimeType('application/json');
req.open('GET', 'https://api.myjson.com/bins/ohp3s', true); // myjson.com url
req.onreadystatechange =
function() {
if (req.readyState == 4 && req.status == "200") {
callback(req.responseText);
}
};
req.send(null);
}
function get(data){
el = document.createElement(data.type);
if(typeof el.id === 'string'){
el.id = data.id;
}
if(typeof el.className === 'string'){
el.className = data.className;
}
if(typeof el.src === 'string'){
el.src = data.src;
}
if(typeof el.href === 'string'){
el.href = data.href;
}
if(typeof el.innerHTML === 'string'){
el.innerHTML = data.innerHTML;
}
if(typeof el.alt === 'string'){
el.alt = data.alt;
}
return el;
}
function recurseJSON(data){
for(let i of data.content) {
console.log(get(i));
if (typeof i.content === 'object'){
recurseJSON(i);
}
}
}
function initJSON() {
loadJSON(function(res) {
recurseJSON(JSON.parse(res));
});
}
initJSON();