I am stuck!..
I have a jQuery script where i am populating a gallery of images from a JSON file, its like a lightbox type of thing.
But i want to have a title to my images, so i have created a second JSON file where i just keep, image name, and title.
I want to, in the loop, check the second JSON if the image name is there, and if it is, return the text that is the title…
This is the titles.json
[
{"Img" : "fun1_001.jpg", "title" : "Title of image 001"},
{"Img" : "fun1_002.jpg", "title" : "title of image 002"},
{"Img" : "fun1_003.jpg", "title" : "title of image 002"}
]
What i want to do is to loop through all images, populate the gallery, and in the same time IF the image name is present in the titles.json, put that title in the loop.
This is the jQuery that populates the gallery. it calls a function named GetTitle(imagename,folder)
//Load the gallery if you click the dropdown.
$(document).on('click','#zmenu>div.item', function(){
$('#dc_preload_image').empty();
let zGallery=$(this).attr('data-value');
$.getJSON('JSON/Gallerys/'+zGallery+'.json', { get_param: 'value' }, (data)=>{
$.each(data, function(index, element) {
console.log('anrop')
$('.galpop-info').galpop();
$("#dc_preload_image").preloader();
$('#dc_preload_image').append('<li><a class="galpop-info" data-galpop-group="info" title="'+GetTitle(element.imageName, zGallery)+'" href="BKND/Galleries/'+zGallery+'/pict/'+element.imageName+'"><img src="BKND/Galleries/'+zGallery+'/tmb/'+element.imageName+'" alt="'+element.imageName+'" id="'+element.imageName+'" class="corners" /></a></li>');
});
});
});
This is the function that should match the image name from the loop and return the title.
function GetTitle(a,z){
$.getJSON('BKND/Galleries/'+z+'/titles.json', (adata) => {
// Handle the JSON data here
let zTitle
$.grep(adata, function(value) {
if(value.Img = a){
zTitle = value.title;
}
else{zTitle = " ";}
});
console.log(zTitle)
return zTitle
});
}
No matter how i try, i cant get it right. i almost solved when i put the code inside function in the loop, but then it iterated through it and created doubles of images in the gallery instead.