I'm using vanilla JavaScript to create a photo carousel; reading in values from an external JSON file, then looping through the JSON data to dynamically add thumbnails, a title, and an onclick to the carousel div. All the images and titles work fine, but the onclick passes only the last value used regardless of which thumbnail is clicked.
Here's the JSON file
var Scenes = {
"Title":["St. Mary Magdalene","Black Mountains","Pen Twyn Glas","Carreg Yr Ogof Cave","Beinn Narnain"],
"Thumbnail":["TNStMary.jpg","TNMountains.jpg","TNPenTwynGlas.jpg","TNCarregYrOgof.jpg","TNBeinnNarnain.jpg"],
"PanoSet":["001x","145+","183+","400x","500x"]
};
Here's the code that is dynamically adding the thumbnails, titles, and onclick.
var Scenes = {
"Title":["St. Mary Magdalene","Black Mountains","Pen Twyn Glas","Carreg Yr Ogof Cave","Beinn Narnain"],
"Thumbnail":["TNStMary.jpg","TNMountains.jpg","TNPenTwynGlas.jpg","TNCarregYrOgof.jpg","TNBeinnNarnain.jpg"],
"PanoSet":["001x","145+","183+","400x","500x"]
};
var Repeat = (Scenes.Title.length);
for (i = 0; i < Repeat; i++) {
NewDiv = document.createElement('div');
NewDiv.id = 'Scene' + i;
document.getElementById('Carousel').appendChild(NewDiv);
document.getElementById('Scene' + i).className = "Scene";
document.getElementById('Scene' + i).style.position = "absolute";
document.getElementById('Scene' + i).style.top = "5px";
document.getElementById('Scene' + i).style.left = 5 + (170 * i) + "px";
document.getElementById('Scene' + i).onclick = function() {
addScene(Scenes.PanoSet[i]);
};
NewDiv = document.createElement('div');
NewDiv.id = 'SceneThumbnail' + i;
document.getElementById('Scene' + i).appendChild(NewDiv);
document.getElementById("SceneThumbnail" + i).className = "SceneThumbnail";
document.getElementById("SceneThumbnail" + i).style.backgroundImage = "url(" + Scenes.Thumbnail[i] + ")";
NewDiv = document.createElement('div');
NewDiv.id = 'SceneTitle' + i;
document.getElementById('Scene' + i).appendChild(NewDiv);
document.getElementById("SceneTitle" + i).className = "SceneTitle";
document.getElementById('SceneTitle' + i).innerHTML = Scenes.Title[i];
}
<div id='Carousel'><div>
This is the line that appears to be the problem (I included all above so as to give context)
document.getElementById('Scene'+i).onclick = function() {addScene(Scenes.PanoSet[i]);};
Which ever of the newly created divs I click on, the code "500x" is passed to my addScene function.
I'm expecting that I've over looked something simple. Can someone offer a solution?
document.getElementById('Scene'+i)over and over, you have the element atNewDivjust use thatiis muted by you loop, so each click has the same reference toi. As above, this is a dupeiyou could also look into event delegation (onclick event listener on the parent element, get what you need from the event target).