I have a json file. I would like to write the content from it into two divs. If json.length % 2 === 0 then write in <div id="data1">
else write it in <div id="data2">
.
I recieve the json file, but it only writes the 1st if sentence.
I hope the code will make more sense then I do =)
var data1 = document.getElementById("data1");
var data2 = document.getElementById("data2");
loadJSON(function(json) {
var l = Object.keys(json).length;
console.log("json start");
for (var i = 0; i <= l; i++){
console.log(i);
if (l % 2 === 0){
for (x in json) {
data1.innerHTML="<img src=" + json[x].picture + "/>";
data1.innerHTML+=json[x].price_thingy.price + json[x].price_thingy.suf;
console.log("0 " + l); // this never prints
}
} else {
for (x in json) {
data2.innerHTML="<img src=" + json[x].picture + "/>";
data2.innerHTML+=json[x].price_thingy.price + json[x].price_thingy.suf;
console.log("1 " + l); // this never prints
}
}
}
});
Edit 1:
So, I've changed l % 2 === 0 to i % 2 === 0 and added innerHTML += and things kind of work now. The problem now, is that I get everything two times. So basically I get the same picture and the price in both divs...
Any idea how I could solve this?