I'm having trouble appending divs and setting their background image based on the number of items in an array or object. Despite adding the divs correctly, all of them end up with the same background image when only three should have it. Can someone explain why this is happening?
JS
Here is the version using an array.
var plane_images = ["images/Top.png", "images/ata21.png", "images/ata26.png", "images/Top.png", "images/Top.png"];
for(var img = 0; img < plane_images.length; img++){
$("#menu").append("<div class='schematics'></div>");
$(".schematics").css("background-image", "url(" +plane_images[img] +")");
console.log(plane_images[img]);
}
This is the object-oriented version.
var Plane_Images = {
top: "images/Top.png",
structure: "images/ata21.png",
electrics: "images/ata26.png",
fuel: "images/Top.png",
hydraulics: "images/Top.png"
}
for(var images in Plane_Images){
var image_src = Plane_Images[images];
$("#menu").append("<div class='schematics'></div>");
$(".schematics").css("background-image", "url(" +image_src +")");
console.log(image_src);
}
It's worth noting that each console log shows the correct values being passed.
HTML
<body>
<div id="header"></div>
<div id="holder">
<div id="menu"></div>
<div id="plane_image"></div>
<div id="sub_menu"></div>
</div>
<div id="footer"></div>
</body>