I am trying to write a very simple script and can't figure out the issue there. the function of the script should be displaying the inner html of each list element but it keeps showing only the last one which is 'Orange'
here is what i have in my html:
<ul id='mylist'>
<li>Red</li>
<li>Green</li>
<li>Black</li>
<li>Orange</li>
</ul>
and below is that script:
var x = document.getElementById('mylist');
var z = x.getElementsByTagName('li');
for (i = 0; i < z.length; i++) {
var res = z[i].innerHTML;
z[i].setAttribute('onclick','alert(res)');
}
I probably need to add a closure here but i am not sure if i really need to and how to add it