- Look at the following code. What does it do? What happens if you click on the li elements?
- Establish what happens when you click on the li elements. Why does this happen?.
- how can the code can be fixed so that it has the expected behavior
<html>
<body>
<ul>
<li>First</li>
<li>Second</li>
<li>Third</li>
<li>Fourth</li>
</ul>
</body>
<script type="text/javascript">
els = document.getElementsByTagName('li');
for(i=0; i < els.length; i++){
els[i].addEventListener('click', function(){alert(i);}, false);
}
</script>
</html>