I'm currently working on iterating over an array and adding an onclick event to each element. My goal is to be able to click on each div and have them log their values in the console. However, I am facing a challenge in applying the onclick event to each individual div. As a beginner in JavaScript, I am unsure why creating a function inside a loop would cause JSBin to raise an issue. Despite trying various approaches, I haven't been able to make progress...
function numberTrack() {
var gridItems = document.getElementsByClassName("grid");
for (var i = 0; i < gridItems[0].length; i ++) {
gridItems.onclick = function(){
alert("hello");
};
}
}
numberTrack();