I am trying to dynamically add onclick function to "li" tagged elements. But the event does not fires. Here is my code:
var arrSideNavButtons = [];
var sideNavLi = document.getElementsByClassName('side-nav')[0].getElementsByTagName('li');
var arrayOfSceneAudios = [scene1Audio, scene2Audio,...];
for (var i = 0; i < sideNavLi.length; i++) {
sideNavLi[i].onclick = function() {
arrayOfSceneAudios[i].play();
}
arrSideNavButtons.push(sideNavLi[i]);
}
Is it possible to code it this way? If yes, what is my mistake? Thanks a lot.
sideNavLiis an empty list. If you are certain that it's not, there is not much else we can say. You should create a jsfiddle.net demo which reproduces the problem.