This is part of a function that is creating a table with images and the name of the image inserted by the user.
var NameFile = [];//Where the names of the files are stored
Function handleFiles() {
var inputElement = document.getElementById("input");
var fileList = inputElement.files;
for(var i = 0; i < fileList.length; i++){
NameFile.push(fileList[i].name);
var Img = document.createElement("tr");
Img.setAttribute("id", "ImgTr" +(i));
document.getElementById("galeria" +(i)).appendChild(Img);
/.../
var Img = document.createElement("tr");
Img.setAttribute("id", "ImgTr" +(i));
document.getElementById("galeria" +(i)).appendChild(Img);
var Imgz = document.createElement("td");
var image =document.createElement("img");
image.setAttribute("id", "imageID" +(i));
image.setAttribute("className", "bordered");
image.setAttribute("src","http://placehold.it/200/200/pink/black");
image.setAttribute("onclick","imgClick(this)");
image.src = window.URL.createObjectURL(fileList[i]);
image.height = 50;
image.with = 50;
image.onload = function(){
window.URL.revokeObjectURL(this.src);
}
Now this is the function i want to call if the image is clicked on. This function is supposed to show a border around the image clicked by the user.
function imgClick(img) {
if (img.className.indexOf('bordered') > -1) {
img.className = img.className.replace('bordered', '').trim();
} else {
img.className += ' bordered';
}
It compares the img clicked id atributte with the others images ids, and when it equals it shows the name of the file stored in the array NameFile
for( var i=0; i<NameFile.length;i++){
if("imageID"+[i]===img.getAttribute("id")){
alert(NameFile[i]);
}
}
}