I have this for loop in a chrome extension that creates image elements based on an api
for (var i = 0; i <= 2; i++) {
//a
var a = document.createElement("a");
//a.setAttribute("href", "embed.html");
a.setAttribute("id", "image"+i);
//img
var img = document.createElement("img");
img.setAttribute("width", "192");
img.setAttribute("height", "108");
img.src = resp.videos[i].thumbnail;
var src = document.getElementById("disp");
img.addEventListener("click", handleClick(i));
a.appendChild(img);
src.appendChild(a);
}
And this function that gets called by the event listener
function handleClick(id) {
chrome.storage.sync.set({'id': id}, function() {
console.log(id);
});
chrome.storage.sync.set({'pathName': pathName}, function() {
console.log(pathName);
});
}
But when one of them is pressed the function all of them get executed and because of this it ends up always saving the last one to local storage.
How can I get the event listener to only run for the one that is pressed?