I've tried to create a loop with for
, and increment by an onclick event, but it doesn't work.
var gameCase = ['', '', '', '', '', '', '', '', ''], // 9
itemLists = $('game').getElementsByTagName('li'); // 9 items
for( var i = 0; i < itemLists.length; i++ ) {
// i already equals 9
itemLists[i].onclick = function() {
// do something
}
}
But in this case, the for
loop is finished before I was able to click on an element in the list.
Moreover, I would like to get the item list I clicked and save it in an array. I tried gameCase[this]
(in onclick function), but I don't know if it's the good way.