I have a JSON array and some JavaScript, but the loop doesn't execute.
I can't find the error.
HTML:
<div class="rosa" id="Mittelt">
// place to append
</div>
JavaScript:
$(function() {
var url = {"cats": [
{"id":"1",
"pictures":"http://www.w3schools.com/css/img_fjords.jpg",
"picsmall":"http://www.w3schools.com/css/img_fjords.jpg"},
{"id":"2",
"pictures":"http://www.w3schools.com/css/img_lights.jpg",
"picsmall":"http://www.w3schools.com/css/img_lights.jpg"}
]
};
var json=url["cats"];
$(json).each(function(item) {
console.log(json[0].id);
item=json[0];
$('<div class="lulu">' +
'<img src="http://www.w3schools.com/css/img_fjords.jpg" data-src="'+item.pictures+'.jpg"/>' +
'<img class="lora" src="'+item.picsmall+'"/>'+'</div>')
.appendTo('#Mittelt');
})
CSS:
.lulu {
position:absolute;
height:100%;
-webkit-transform: translateZ(0px);
-ms-transform: translateZ(0px);
-o-transform: translateZ(0px);
transform: translateZ(0px);
}
.lora {
position:absolute;
height:50%;
-webkit-transform: translateZ(0px);
-ms-transform: translateZ(0px);
-o-transform: translateZ(0px);
transform: translateZ(0px);
}
Here's a link: https://jsfiddle.net/5wyL5azj/2/
itemso it always points to the first element:item=json[0];Remove that line.$(json).each(function(item){to$(json).each(function(i, item){divs and their contents are there.