I have an array of objects and I am building a page with those objecs The objects have an image, image will be clickable and I want to count the clicks per image. I am trying to assign the value for clicks from each object to "tclicks" and than hand "tclicks" over to the onclick function to count the clicks separately. I am not sure if that works.
My current problem is that the value appears as NaN when the onclick function gets executed. I am aware that I need to assign a starting value to clicks. I tried it in various places. In the object, in the function outside of the function. Either the value was not counting up, or I got an error as the element was set to 0 Where can I assign the starting value?
This is the array
var things =
[
{img : "cat.jpg",
tclicks: "cleo_clicks",
id : "cleo"
},
{img : "shimi.png",
tclicks: "shimi_clicks",
id : "shimi"
}
]
This is how I am building the page for ( var i= 0; i < things.length; i++){
var x = document.createElement("IMG");
x.setAttribute("src", things[i].img);
x.setAttribute(tclicks, things[i].clicks);
x.setAttribute("onclick", "countClicks(tclicks)");
document.body.appendChild(x);
}
And this is my onclick functions
function countClicks(clicks){
clicks ++;
console.log(clicks)
}