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.
3
Answers
Ok. so this is how far i have gotten, so far.
Now it works more like i intended, i dont bombard the JSON file (probobly speeding up the process, and instead work with it as an object. all images is processed (ive checked with a console.log), but i think there is something wrong with this line
because all images is coming back with the line 'unfortunately unknown' Is there a better way to do this, please, inform an uninformed :)
Your problem already seems to have an answer on Stackoverflow. You can use a callback (you give a function as a parameter to your function and you call the callback function with the title parameter instead of returning the title). Otherwise you can use a promise but you’ll need to use async.
An example of callback:
And how to use it:
(This is a major rewrite of my answer – you can find previous versions of this post here.)
Your original attempt would involve frequent
$.getJSON('BKND/Galleries/'+z+'/titles.json)
calls (at least one call for each image in your gallery) to get the titles. A better approach would be to get the information once and store it in a dictionary. Below I prepared a little demo, based on publicly available data, mimicking a very basic gallery:Update, in response to OP’s "answer" post:
You can use the
async:false
option in an Ajax call but that will create a negative user experience. The browser will effectively freeze while it is receiving the data.Nonetheless, I have taken your code and tweaked it in some places to make it work. The demo uses https://dummyjson.com as a public data source.
(( I would still recommend you to use the
$.when(...).then()
construct as demonstrated in my snippet above. ))Apart from not using
async:false
I would also recommend you to replace the frequent.append()
calls by a single.html()
call. This way the complete html markdown will be parsed and rendered only once by the browser instead of the page being re-rendered for each individual image.I could not resist and wrote the
async
version with$.when(...).then()
, see below: