I'm trying to:
Fetch JSON data using $.get
append that data to an HTML element $('photos') so that images are displayed using the external JSON file instead of in-line HTML.
I'm not getting any console errors and the images aren't displaying. Any help would be EXTREMELY appreciated. Thanks a lot. I have an alert to check if the JSON is loaded, and it loads fine- just doesn't change any data.
Here is my current setup:
(JSON): photoData.json
{ "photos": {
"1": {
"href": "./photos/photo1.JPG",
"src": "photos/photo1.JPG",
},
"2": {
"href": "./photos/photo2.JPG",
"src": "photos/photo2.JPG",
},
"3": {
"href": "./photos/photo3.JPG",
"src": "photos/photo3.JPG",
},
"4": {
"href": "./photos/photo4.JPG",
"src": "photos/photo4.JPG",
},
"5": {
"href": "./photos/photo5.JPG",
"src": "photos/photo5.JPG",
}}}
jQuery:
$.get("./data/photoData.json", function(photos){
alert("Data Loaded: " + photos);
let htmlStr = "";
for (let i = 0; i < photos.length; i++){
htmlStr += `<figure><a href="${photos[i].href}"><img src = "${photos[i].src}"></img></a></figure>`
}
$('Photos').html(htmlStr);});
photos.lengthwhenphotosis actually an object. Usefor..ininstead.